:root {
  --ink: #172026;
  --muted: #65737d;
  --line: #d8e2e4;
  --bg: #edf4f2;
  --panel: #ffffff;
  --paper: #fbfcfb;
  --teal: #08746f;
  --teal-dark: #055b57;
  --blue: #2f6f9f;
  --coral: #bf5b48;
  --gold: #a87915;
  --green-soft: #e7f4ef;
  --red-soft: #fbebe7;
  --shadow: 0 18px 42px rgba(22, 38, 44, .08);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--ink);
  background: var(--bg);
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; }

button {
  border: 0;
  border-radius: 8px;
  min-height: 40px;
  padding: 9px 13px;
  background: var(--teal);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

button:hover { background: var(--teal-dark); }
button.secondary { background: #e8efef; color: var(--ink); }
button.secondary:hover { background: #dce7e6; }
button.warn { background: var(--coral); }
button.warn:hover { background: #a64e3e; }
button:disabled { opacity: .55; cursor: not-allowed; }

header {
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.topbar {
  max-width: 1500px;
  margin: 0 auto;
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--teal), var(--blue));
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.1;
}

.status {
  color: var(--muted);
  font-size: 13px;
  text-align: right;
  min-height: 18px;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

main {
  max-width: 1500px;
  margin: 0 auto;
  padding: 14px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(280px, .82fr) minmax(420px, 1.18fr) minmax(420px, 1.22fr);
  gap: 16px;
  align-items: start;
}

.mobile-tabs {
  display: none;
}

body.force-mobile-view .mobile-tabs {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.96);
  box-shadow: var(--shadow);
}

body.force-mobile-view .layout {
  display: block;
}

body.force-mobile-view .tab-panel {
  display: none;
}

body.force-mobile-view .tab-panel.active {
  display: block;
}

body.force-mobile-view .right-stack {
  display: contents;
}

body.force-mobile-view .tab-btn {
  min-height: 36px;
  padding: 6px 4px;
  background: #e8efef;
  color: var(--ink);
  font-size: 12px;
}

body.force-mobile-view .tab-btn.active {
  background: var(--teal);
  color: #fff;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-head {
  min-height: 54px;
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: #fbfdfd;
}

h2 {
  margin: 0;
  font-size: 16px;
  line-height: 1.2;
}

h3 {
  margin: 18px 0 9px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
}

.panel-body { padding: 14px; }

.live-card {
  position: sticky;
  top: 8px;
  z-index: 5;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  box-shadow: var(--shadow);
}

.record-main {
  min-height: 54px;
  font-size: 16px;
  flex: 1 1 130px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  margin-bottom: 10px;
}

input[type="text"], input[type="password"], input[type="file"], select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 10px;
  min-height: 40px;
  outline: none;
}

textarea {
  resize: vertical;
  line-height: 1.45;
  min-height: 120px;
}

.row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.row > * { flex: 1 1 auto; }
.row button { flex: 0 0 auto; }

.range-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--teal);
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 9px;
  color: var(--muted);
  background: #fff;
  font-size: 12px;
  font-weight: 700;
}

.record-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #b7c2c6;
  display: inline-block;
  margin-right: 7px;
}

.record-dot.on {
  background: var(--coral);
  box-shadow: 0 0 0 4px rgba(191, 91, 72, .16);
}

.conversation {
  height: calc(100vh - 142px);
  min-height: 650px;
  overflow: auto;
  padding: 8px;
  background: #f5f8f7;
}

.searchbar {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.empty {
  min-height: 320px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
  padding: 24px;
}

.turn {
  border: 1px solid var(--line);
  border-left: 4px solid var(--teal);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
  margin-bottom: 7px;
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 8px;
}

.turn.patient { border-left-color: var(--gold); }
.turn.parent { border-left-color: var(--blue); }
.turn.interim { opacity: .7; }

.turn-meta {
  display: grid;
  gap: 8px;
  align-content: start;
}

.stamp {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.speaker {
  font-size: 12px;
  padding: 5px 6px;
  min-height: 30px;
}

.turn textarea {
  min-height: 46px;
  font-size: 13px;
  padding: 7px 8px;
}

.right-stack {
  display: grid;
  gap: 16px;
}

.letter-preview {
  border: 1px solid #d6dde0;
  border-radius: 8px;
  background: #fff;
  padding: 18px;
  margin-bottom: 12px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.015);
}

.letter-sheet {
  width: 100%;
  min-height: 520px;
  background: #fff;
  color: #1d2529;
  border: 1px solid #e1e7e9;
  padding: 28px 34px;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1.45;
}

.letter-top {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 18px;
  align-items: start;
  border-bottom: 2px solid #597f37;
  padding-bottom: 14px;
  margin-bottom: 18px;
}

.hospital-title {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0;
  color: #324047;
  text-transform: uppercase;
}

.service-title {
  font-family: Arial, Helvetica, sans-serif;
  margin-top: 4px;
  color: #597f37;
  font-size: 12px;
  font-weight: 700;
}

.doctor-block {
  text-align: right;
  font-size: 12px;
  color: #34434a;
}

.letter-title {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 18px;
  font-weight: 800;
  text-align: center;
  margin: 12px 0 18px;
}

.identity-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 20px;
  padding: 10px 12px;
  border: 1px solid #dfe6e8;
  background: #fbfcfc;
  font-size: 13px;
  margin-bottom: 18px;
  font-family: Arial, Helvetica, sans-serif;
}

.letter-body {
  white-space: pre-wrap;
  font-size: 13.6px;
}

.letter-body .section-title {
  display: block;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: 800;
  color: #1c3038;
  margin: 13px 0 4px;
}

.verify-mark {
  color: #b3261e;
  font-weight: 800;
}

.medical-check {
  font-weight: 800;
}

.signature {
  margin-top: 26px;
  display: flex;
  justify-content: flex-end;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  white-space: pre-line;
  text-align: left;
}

.report {
  min-height: 430px;
  background: var(--paper);
  font-size: 14px;
}

.report-source {
  display: none;
}

.report-rich-editor {
  min-height: 430px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  padding: 10px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  outline: none;
  overflow: auto;
}

.edit-label {
  margin-top: 10px;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.doc-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.doc-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.notice {
  border-radius: 8px;
  padding: 10px;
  color: #45535b;
  background: var(--green-soft);
  font-size: 13px;
  line-height: 1.4;
  margin: 10px 0;
}

.notice.warn { background: var(--red-soft); }

.debug-log {
  min-height: 70px;
  max-height: 150px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #172026;
  color: #dbe8e8;
  padding: 9px 10px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  white-space: pre-wrap;
}

.two {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

code {
  background: rgba(255,255,255,.7);
  border: 1px solid rgba(0,0,0,.06);
  border-radius: 5px;
  padding: 1px 4px;
}

@media (max-width: 1180px) {
  .layout { grid-template-columns: 1fr; }
  .conversation, .report {
    height: auto;
    min-height: 520px;
  }
}

@media (max-width: 640px) {
  body { background: #f4f8f7; }
  h1 { font-size: 17px; }
  .mark { width: 32px; height: 32px; }
  main, .topbar {
    padding-left: 8px;
    padding-right: 8px;
  }
  main { padding-top: 8px; }
  .layout { gap: 10px; }
  .mobile-tabs {
    position: sticky;
    top: 0;
    z-index: 20;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    padding: 4px;
    margin-bottom: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255,255,255,.96);
    box-shadow: var(--shadow);
  }
  .tab-btn {
    min-height: 36px;
    padding: 6px 4px;
    background: #e8efef;
    color: var(--ink);
    font-size: 12px;
  }
  .tab-btn.active {
    background: var(--teal);
    color: #fff;
  }
  .tab-panel {
    display: none;
  }
  .tab-panel.active {
    display: block;
  }
  .right-stack {
    display: contents;
  }
  .panel { border-radius: 8px; }
  .panel-head { min-height: 46px; padding: 10px 11px; }
  .panel-body { padding: 10px; }
  .two { grid-template-columns: 1fr; }
  .turn {
    grid-template-columns: 72px 1fr;
    padding: 7px;
  }
  .turn textarea { min-height: 40px; }
  .conversation {
    min-height: 430px;
    height: 56vh;
  }
  .report { min-height: 360px; }
  .notice { display: none; }
  .view-toggle { display: none; }
  .letter-preview { padding: 8px; }
  .letter-sheet { padding: 18px 16px; }
  .identity-grid { grid-template-columns: 1fr; }
}

@media print {
  header,
  .mobile-tabs,
  .layout > .panel:first-child,
  .layout > .panel:nth-child(2),
  .panel-head,
  .panel-body.printable > .two,
  #reportEditor,
  .edit-label,
  .report-rich-editor,
  .report-source {
    display: none;
  }
  body, main, .layout, .panel, .right-stack {
    display: block;
    background: #fff;
    box-shadow: none;
    border: 0;
    padding: 0;
    margin: 0;
    max-width: none;
  }
  .right-stack .panel:first-child { display: block; }
  .report {
    border: 0;
    min-height: 0;
    height: auto;
    resize: none;
    overflow: visible;
    font-size: 12pt;
    white-space: pre-wrap;
  }

  .letter-preview {
    display: block !important;
    border: 0;
    box-shadow: none;
    padding: 0;
    margin: 0;
  }

  .letter-sheet {
    border: 0;
    padding: 0;
    min-height: 0;
  }

  .report {
    display: none;
  }
}
