#panel-cases {
  background: #f7f8fa;
  min-height: calc(100vh - 120px);
  padding: 18px;
}

#case-root {
  color: #1f2937;
  font-size: 14px;
}

.case-layout {
  display: grid;
  grid-template-columns: minmax(250px, 330px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.case-mobile-selector,
.case-list-backdrop,
.case-sidebar-close { display: none; }

.case-sidebar,
.case-detail {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}

.case-sidebar {
  position: sticky;
  top: 12px;
  overflow: hidden;
}

.case-sidebar-head,
.case-detail-header,
.case-timeline-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.case-sidebar-head {
  padding: 17px 17px 12px;
}

.case-sidebar-head h2,
.case-detail-header h2,
.case-timeline-header h3 {
  margin: 0;
  color: #111827;
}

.case-sidebar-head h2 { font-size: 19px; }
.case-sidebar-head p,
.case-timeline-header p {
  margin: 3px 0 0;
  color: #6b7280;
  font-size: 12px;
}

.case-create-actions {
  display: grid;
  gap: 7px;
  padding: 0 17px 15px;
  border-bottom: 1px solid #eef0f3;
}

.case-btn {
  min-height: 34px;
  padding: 7px 12px;
  border: 1px solid transparent;
  border-radius: 6px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.case-btn:disabled {
  cursor: wait;
  opacity: 0.62;
}

.case-btn.primary {
  color: #fff;
  background: #1f2937;
}

.case-btn.primary:hover:not(:disabled) { background: #111827; }

.case-btn.secondary {
  color: #374151;
  background: #fff;
  border-color: #d1d5db;
}

.case-btn.secondary:hover:not(:disabled) { background: #f9fafb; }

.case-btn.danger {
  color: #b91c1c;
  background: #fff;
  border-color: #fecaca;
}

.case-btn.small {
  min-height: 30px;
  padding: 5px 10px;
  font-size: 12px;
}

.case-list {
  max-height: calc(100vh - 260px);
  overflow-y: auto;
  padding: 9px;
}

.case-list-item {
  display: block;
  width: 100%;
  margin: 0 0 7px;
  padding: 12px;
  color: inherit;
  text-align: left;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  cursor: pointer;
}

.case-list-item:hover { background: #f9fafb; }
.case-list-item.selected {
  background: #eff6ff;
  border-color: #93c5fd;
  box-shadow: inset 3px 0 0 #2563eb;
}

.case-list-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.case-list-title {
  overflow: hidden;
  font-size: 14px;
  line-height: 1.45;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.case-list-complaint {
  display: -webkit-box;
  overflow: hidden;
  margin: 7px 0;
  color: #4b5563;
  font-size: 12px;
  line-height: 1.45;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.case-list-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 9px;
  color: #6b7280;
  font-size: 11px;
}

.case-list-empty,
.case-timeline-empty {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 24px 14px;
  color: #6b7280;
  text-align: center;
}

.case-list-empty strong,
.case-timeline-empty strong { color: #374151; }

.case-detail {
  min-height: 520px;
  padding: 20px;
}

.case-detail-header {
  padding-bottom: 17px;
  border-bottom: 1px solid #eef0f3;
}

.case-detail-header h2 {
  font-size: 21px;
  line-height: 1.4;
}

.case-detail-badges,
.case-event-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 7px;
}

.case-badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 2px 8px;
  border-radius: 999px;
  color: #4b5563;
  background: #f3f4f6;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.case-badge.status-active { color: #047857; background: #d1fae5; }
.case-badge.status-paused { color: #b45309; background: #fef3c7; }
.case-badge.status-closed { color: #475569; background: #e2e8f0; }
.case-badge.status-archived { color: #6b7280; background: #f3f4f6; }
.case-badge.retrospective { color: #92400e; background: #fef3c7; }

.case-read-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px 18px;
  margin: 18px 0;
}

.case-read-field {
  min-width: 0;
  padding: 11px 12px;
  background: #f9fafb;
  border-radius: 7px;
}

.case-read-field.wide { grid-column: 1 / -1; }
.case-read-field dt {
  margin-bottom: 4px;
  color: #6b7280;
  font-size: 11px;
  font-weight: 600;
}

.case-read-field dd {
  margin: 0;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.case-read-field dd.missing,
.case-muted { color: #9ca3af; }

.case-completeness {
  margin: 0 0 20px;
  padding: 13px 14px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.case-completeness h4 { margin: 0 0 9px; }
.case-completeness p { margin: 0; }
.case-completeness-items {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.case-completeness-items span {
  padding: 4px 8px;
  border-radius: 5px;
  font-size: 12px;
}

.case-completeness-items .complete { color: #047857; background: #ecfdf5; }
.case-completeness-items .incomplete { color: #9ca3af; background: #f9fafb; }

.case-timeline-header {
  margin-top: 8px;
  padding: 18px 0 13px;
  border-top: 1px solid #eef0f3;
}

.case-timeline-header h3 { font-size: 17px; }
.case-timeline { position: relative; }

.case-event-card {
  position: relative;
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  gap: 10px;
  padding-bottom: 13px;
}

.case-event-card::before {
  position: absolute;
  top: 17px;
  bottom: -5px;
  left: 7px;
  width: 2px;
  background: #dbe2ea;
  content: '';
}

.case-event-card:last-child::before { display: none; }

.case-timeline-marker {
  position: relative;
  z-index: 1;
  width: 14px;
  height: 14px;
  margin-top: 13px;
  background: #fff;
  border: 3px solid #64748b;
  border-radius: 50%;
  box-sizing: border-box;
}

.case-event-card.expanded .case-timeline-marker { border-color: #2563eb; }
.case-event-card.retrospective .case-timeline-marker { border-color: #d97706; }

.case-event-body {
  overflow: hidden;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.case-event-card.retrospective .case-event-body {
  background: #fffbeb;
  border-color: #fde68a;
}

.case-event-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  width: 100%;
  padding: 12px 14px;
  color: inherit;
  text-align: left;
  background: transparent;
  border: 0;
  cursor: pointer;
}

.case-event-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 5px 12px;
}

.case-event-title strong { color: #111827; }
.case-event-title span { color: #6b7280; font-size: 12px; }
.case-event-meta { justify-content: flex-end; margin-top: 0; }
.case-event-chevron { color: #6b7280; font-size: 11px; align-self: center; }

.case-event-content {
  padding: 4px 14px 14px;
  border-top: 1px solid #eef0f3;
}

.case-retrospective-note {
  margin: 11px 0;
  padding: 8px 10px;
  color: #92400e;
  background: #fef3c7;
  border-radius: 5px;
  font-size: 12px;
}

.case-assessment-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 11px 0;
  padding: 9px 10px;
  background: #eff6ff;
  border-radius: 6px;
  font-size: 12px;
}

.case-assessment-link.unavailable { color: #991b1b; background: #fef2f2; }
.case-link-button {
  padding: 0;
  color: #1d4ed8;
  text-align: right;
  background: transparent;
  border: 0;
  cursor: pointer;
  text-decoration: underline;
}

.case-event-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
  margin-top: 10px;
}

.case-event-field {
  min-width: 0;
  padding: 10px;
  background: rgba(249, 250, 251, 0.86);
  border-radius: 6px;
}

.case-event-field h4 {
  margin: 0 0 5px;
  color: #6b7280;
  font-size: 11px;
}

.case-event-field p {
  margin: 0;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.case-event-missing {
  margin: 10px 0 0;
  color: #9ca3af;
  font-size: 11px;
  line-height: 1.5;
}

.case-event-actions {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
  margin-top: 12px;
}

.case-message {
  display: none;
  margin: 0 0 12px;
  padding: 9px 12px;
  border-radius: 6px;
}

.case-message.visible { display: block; }
.case-message.success { color: #065f46; background: #d1fae5; }
.case-message.error { color: #991b1b; background: #fee2e2; }

.case-loading,
.case-detail-empty,
.case-page-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 260px;
  padding: 28px;
  color: #6b7280;
  text-align: center;
}

.case-page-empty {
  min-height: calc(100vh - 190px);
  background: #fff;
  border: 1px dashed #d1d5db;
  border-radius: 10px;
}

.case-detail-empty h3,
.case-page-empty h2 { margin: 0 0 7px; color: #374151; }
.case-detail-empty p,
.case-page-empty p { margin: 0 0 16px; }

.case-modal-backdrop {
  position: fixed;
  z-index: 9000;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 38px 18px;
  overflow-y: auto;
  background: rgba(15, 23, 42, 0.55);
}

.case-modal {
  width: min(840px, 100%);
  max-height: calc(100vh - 76px);
  overflow-y: auto;
  background: #fff;
  border-radius: 11px;
  box-shadow: 0 20px 55px rgba(15, 23, 42, 0.24);
}

.case-modal-header {
  position: sticky;
  z-index: 2;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 18px;
  background: #fff;
  border-bottom: 1px solid #e5e7eb;
}

.case-modal-header h3 { margin: 0; font-size: 18px; }
.case-modal-close {
  width: 32px;
  height: 32px;
  padding: 0;
  color: #6b7280;
  background: transparent;
  border: 0;
  font-size: 23px;
  cursor: pointer;
}

.case-confirm-modal {
  width: min(430px, 100%);
  padding: 22px;
}

.case-confirm-modal h3 { margin: 0 0 8px; }
.case-confirm-modal p { margin: 0; color: #4b5563; line-height: 1.55; }
.case-confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.case-form { padding: 17px 18px 20px; }
.case-form-section {
  margin: 0 0 15px;
  padding: 13px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
}

.case-form-section legend {
  padding: 0 6px;
  color: #374151;
  font-weight: 700;
}

.case-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.case-form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}

.case-form-group.wide { grid-column: 1 / -1; }
.case-form-group > span { color: #4b5563; font-size: 12px; font-weight: 600; }

.case-form input,
.case-form select,
.case-form textarea {
  width: 100%;
  min-height: 36px;
  padding: 8px 9px;
  color: #111827;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  box-sizing: border-box;
  font: inherit;
}

.case-form textarea { resize: vertical; }
.case-form input:focus,
.case-form select:focus,
.case-form textarea:focus {
  outline: 2px solid #bfdbfe;
  border-color: #60a5fa;
}

.case-form-hint {
  margin: 7px 0 0;
  color: #6b7280;
  font-size: 11px;
  line-height: 1.5;
}

.case-form-hint.retrospective-hint {
  padding: 8px 10px;
  color: #92400e;
  background: #fef3c7;
  border-radius: 5px;
}

.case-legacy-banner {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 15px;
  padding: 12px 13px;
  color: #92400e;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 7px;
}

.case-legacy-banner span { font-size: 12px; }
.case-initial-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 3px 0 14px;
  font-weight: 600;
}

.case-initial-toggle input { width: auto; min-height: 0; }
.case-completeness-edit {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.case-completeness-edit label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  background: #f9fafb;
  border-radius: 5px;
}

.case-completeness-edit input { width: auto; min-height: 0; }
.case-form-actions {
  position: sticky;
  bottom: -20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 4px -18px -20px;
  padding: 13px 18px;
  background: #fff;
  border-top: 1px solid #e5e7eb;
}

.case-form-error {
  margin: 0 0 12px;
  color: #b91c1c;
  font-size: 12px;
}

@media (max-width: 900px) {
  #panel-cases { padding: 12px; }
  .case-layout { grid-template-columns: 1fr; }
  .case-sidebar { position: static; }
  .case-list { max-height: 330px; }
}

@media (max-width: 640px) {
  #panel-cases { padding: 8px; }
  .case-detail { padding: 14px; }
  .case-detail-header,
  .case-timeline-header,
  .case-event-toggle { align-items: flex-start; flex-direction: column; }
  .case-read-grid,
  .case-event-fields,
  .case-form-grid { grid-template-columns: 1fr; }
  .case-read-field.wide,
  .case-form-group.wide { grid-column: auto; }
  .case-completeness-edit { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .case-modal-backdrop { padding: 10px; }
  .case-modal { max-height: calc(100vh - 20px); }
  .case-form { padding: 13px 12px 18px; }
  .case-form-actions { margin-right: -12px; margin-left: -12px; padding-right: 12px; padding-left: 12px; }
}

/* R-43: mobile Case selector, timeline and safe-area handling */
@media (max-width: 768px) {
  #panel-cases {
    min-height: calc(100vh - 152px);
    min-height: calc(100dvh - 152px);
    padding: 8px;
  }

  .case-layout { display: block; }

  .case-mobile-selector {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 10px;
    padding: 10px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #fff;
  }
  .case-mobile-selector > div { min-width: 0; }
  .case-mobile-selector span,
  .case-mobile-selector strong {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .case-mobile-selector span { margin-bottom: 2px; color: #6b7280; font-size: 11px; }
  .case-mobile-selector strong { color: #111827; font-size: 14px; }
  .case-mobile-selector .case-btn { flex: 0 0 auto; }

  .case-list-backdrop {
    position: fixed;
    z-index: 1160;
    inset: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    padding: 0;
    border: 0;
    background: rgba(15, 23, 42, .5);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  .case-layout.case-list-open .case-list-backdrop {
    display: block;
    opacity: 1;
    pointer-events: auto;
  }

  .case-sidebar {
    position: fixed;
    z-index: 1170;
    inset: auto 0 0;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-height: 78vh;
    max-height: 78dvh;
    padding-bottom: env(safe-area-inset-bottom);
    border-radius: 14px 14px 0 0;
    transform: translate3d(0, 105%, 0);
    transition: transform .22s ease;
  }
  .case-layout.case-list-open .case-sidebar { transform: translate3d(0, 0, 0); }
  .case-sidebar-head { flex: 0 0 auto; padding-top: 14px; }
  .case-sidebar-close {
    display: grid;
    place-items: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    color: #6b7280;
    background: #f3f4f6;
    font-size: 24px;
  }
  .case-list { flex: 1; min-height: 0; max-height: none; overscroll-behavior: contain; }
  .case-list-item { min-height: 58px; }

  .case-detail { width: 100%; min-width: 0; padding: 14px; }
  .case-detail-header,
  .case-timeline-header,
  .case-event-toggle { align-items: stretch; flex-direction: column; }
  .case-detail-header .case-btn,
  .case-timeline-header .case-btn,
  .case-event-actions .case-btn { min-height: 44px; }
  .case-event-card { padding-left: 28px; }
  .case-event-card::before { left: 10px; }
  .case-timeline-marker { left: 4px; }
  .case-event-body { min-width: 0; }
  .case-event-meta { justify-content: flex-start; }
  .case-event-fields,
  .case-read-grid,
  .case-form-grid { grid-template-columns: minmax(0, 1fr); }
  .case-read-field.wide,
  .case-form-group.wide { grid-column: auto; }

  .case-btn { min-height: 44px; touch-action: manipulation; }
  .case-btn.small { min-height: 44px; padding: 8px 10px; }
  .case-form input,
  .case-form select,
  .case-form textarea { min-height: 44px; font-size: 16px; }
  .case-initial-toggle,
  .case-completeness-edit label { min-height: 44px; }

  .case-modal-backdrop {
    padding: calc(10px + env(safe-area-inset-top)) 10px calc(10px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }
  .case-modal {
    max-height: calc(100vh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    max-height: calc(100dvh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
  .case-modal-close { width: 44px; height: 44px; }
  .case-form-actions { bottom: 0; padding-bottom: calc(13px + env(safe-area-inset-bottom)); }
}
