:root {
  color-scheme: light dark;
  --bg: #f6f5f2;
  --panel: #ffffff;
  --text: #1f2320;
  --muted: #6b7066;
  --border: #e2e0da;
  --accent: #2f6f4f;
  --accent-text: #ffffff;
  --doctor-bubble: #ffffff;
  --patient-bubble: #2f6f4f;
  --patient-text: #ffffff;
  --danger: #b23b3b;
  --warn-bg: #fff6e0;
  --warn-border: #e8cf8a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181a;
    --panel: #1e2225;
    --text: #eae7e0;
    --muted: #9aa19b;
    --border: #33383b;
    --accent: #4d9b73;
    --accent-text: #0c120e;
    --doctor-bubble: #262b2e;
    --patient-bubble: #3a7d5a;
    --patient-text: #f4fff8;
    --warn-bg: #2a2612;
    --warn-border: #5c4d1a;
  }
}

:root[data-theme="light"] {
  --bg: #f6f5f2; --panel: #ffffff; --text: #1f2320; --muted: #6b7066; --border: #e2e0da;
  --accent: #2f6f4f; --accent-text: #ffffff; --doctor-bubble: #ffffff; --patient-bubble: #2f6f4f;
  --patient-text: #ffffff; --warn-bg: #fff6e0; --warn-border: #e8cf8a;
}
:root[data-theme="dark"] {
  --bg: #15181a; --panel: #1e2225; --text: #eae7e0; --muted: #9aa19b; --border: #33383b;
  --accent: #4d9b73; --accent-text: #0c120e; --doctor-bubble: #262b2e; --patient-bubble: #3a7d5a;
  --patient-text: #f4fff8; --warn-bg: #2a2612; --warn-border: #5c4d1a;
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win over any component's own `display` rule,
   no matter its specificity or source order — otherwise toggling `.hidden = true/false`
   in app.js silently fails to actually hide the element. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.brand {
  font-weight: 700;
  font-size: 1.1rem;
}

.disclaimer {
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  background: var(--warn-bg);
  border-bottom: 1px solid var(--warn-border);
  padding: 0.6rem 1rem;
}

main.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#setupScreen {
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  max-width: 560px;
  width: 100%;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.card h1 {
  font-size: 1.25rem;
  margin: 0 0 1rem;
}

.mode-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  margin-bottom: 1.2rem;
}

.mode-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 12px;
  padding: 0.7rem 0.6rem;
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  text-align: left;
}

.mode-btn span {
  font-weight: 400;
  font-size: 0.78rem;
  color: var(--muted);
}

.mode-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent);
}

.mode-fields {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  margin-bottom: 1.2rem;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

input[type="text"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.7rem;
  resize: vertical;
}

textarea:focus, input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.primary-btn {
  width: 100%;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 12px;
  padding: 0.75rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.primary-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 0.4rem 0.8rem;
  font-size: 0.82rem;
  cursor: pointer;
}

#chatScreen {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.msg {
  max-width: 82%;
  padding: 0.6rem 0.85rem;
  border-radius: 14px;
  line-height: 1.55;
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.doctor {
  align-self: flex-end;
  background: var(--doctor-bubble);
  border: 1px solid var(--border);
  border-bottom-right-radius: 4px;
}

.msg.patient {
  align-self: flex-start;
  background: var(--patient-bubble);
  color: var(--patient-text);
  border-bottom-left-radius: 4px;
}

.msg.system-note {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  max-width: 100%;
  text-align: center;
}

.msg.error {
  align-self: center;
  background: transparent;
  color: var(--danger);
  font-size: 0.85rem;
  border: 1px dashed var(--danger);
}

.msg .label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  opacity: 0.65;
  margin-bottom: 0.2rem;
}

.msg.doctor.typing::after {
  content: "▍";
  animation: blink 1s step-start infinite;
}

@keyframes blink { 50% { opacity: 0; } }

.report table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5rem 0;
  font-size: 0.88rem;
}
.report th, .report td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  text-align: left;
  vertical-align: top;
}
.report h2 {
  font-size: 1.02rem;
  margin: 1rem 0 0.4rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.2rem;
}
.report h2:first-child { margin-top: 0; }
.report ul, .report ol { margin: 0.3rem 0; padding-left: 1.3rem; }
.report p { margin: 0.4rem 0; }

.composer {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 0.6rem 0.8rem 0.8rem;
}

.composer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.4rem;
}

.speak-toggle {
  flex-direction: row;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.8rem;
}
.speak-toggle input { width: auto; }

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

#textInput {
  flex: 1;
  max-height: 8rem;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 50%;
  width: 2.6rem;
  height: 2.6rem;
  min-width: 2.6rem;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn.send {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.icon-btn.recording {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}

.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.mic-status {
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.3rem;
}

@media (max-width: 480px) {
  .mode-toggle { grid-template-columns: 1fr; }
  .msg { max-width: 90%; }
}
