:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --teal: #0f766e;
  --teal-dark: #115e59;
  --teal-light: #f0fdfa;
  --teal-border: #99f6e4;
  --danger: #dc2626;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 6px 20px rgba(15, 23, 42, 0.10);
  --radius: 10px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  font-size: 14px;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
}

/* Top Bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 20;
  flex-shrink: 0;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  flex-shrink: 0;
}

.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: -0.3px;
  color: var(--teal-dark);
  line-height: 1.1;
}

.brand-sub {
  font-size: 11px;
  color: var(--text-muted);
  display: block;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

.divider-v {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 2px;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

.icon-btn, .action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 36px;
  padding: 6px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  white-space: nowrap;
}

.icon-btn:hover:not(:disabled), .action-btn:hover:not(:disabled) {
  background: var(--surface-alt);
  border-color: #cbd5e1;
}

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

.action-btn.primary {
  background: var(--teal);
  color: #ffffff;
  border-color: var(--teal);
}

.action-btn.primary:hover {
  background: var(--teal-dark);
  border-color: var(--teal-dark);
}

.action-btn.teal-outline {
  color: var(--teal);
  border-color: var(--teal);
  background: var(--teal-light);
}

.icon-btn.danger-ghost:hover {
  color: var(--danger);
  border-color: #fecaca;
  background: #fef2f2;
}

.full-width {
  width: 100%;
}

/* View Bar */
.view-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 14px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.view-pills {
  display: flex;
  align-items: center;
  gap: 5px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 1px 0;
}

.view-pills::-webkit-scrollbar {
  display: none;
}

.pill-sep {
  width: 1px;
  height: 18px;
  background: #cbd5e1;
  margin: 0 3px;
  flex-shrink: 0;
}

.view-pill {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  font-weight: 600;
  color: #475569;
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 30px;
}

.view-pill.active {
  background: var(--teal);
  color: #ffffff;
  border-color: var(--teal);
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.zoom-btn {
  min-width: 30px;
  height: 30px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 700;
  font-size: 14px;
}

.zoom-btn.text {
  padding: 0 8px;
  font-size: 12px;
}

/* Workspace 3-pane */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 276px minmax(0, 1fr) 300px;
  min-height: 0;
  overflow: hidden;
}

.tool-sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.right-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-scroll, .right-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.tool-section, .panel-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.panel-card {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
}

.section-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.section-title {
  font-size: 11.5px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: #475569;
}

.mini-btn {
  padding: 3px 8px;
  border-radius: 6px;
  border: 1px solid var(--teal-border);
  background: var(--teal-light);
  color: var(--teal);
  font-size: 11.5px;
  font-weight: 700;
}

.segmented {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1fr;
  gap: 4px;
  background: var(--bg);
  padding: 3px;
  border-radius: 9px;
}

.seg-btn {
  padding: 6px 4px;
  border-radius: 6px;
  font-size: 11.5px;
  font-weight: 700;
  color: #475569;
  min-height: 32px;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--teal-dark);
  box-shadow: var(--shadow-sm);
}

.tool-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tool-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  min-height: 40px;
  padding: 7px 10px;
  border-radius: 9px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  text-align: left;
  transition: border-color 0.14s, background 0.14s, box-shadow 0.14s;
}

.tool-item:hover {
  background: var(--surface-alt);
}

.tool-item.active {
  border-color: var(--teal);
  background: var(--teal-light);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.12);
}

.tool-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.tool-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex-shrink: 0;
}

.tool-line-preview {
  width: 20px;
  height: 4px;
  border-radius: 2px;
  flex-shrink: 0;
}

.tool-names {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.tool-name-en {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.15;
}

.tool-name-zh {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.15;
}

.tool-del-btn {
  padding: 2px 6px;
  border-radius: 5px;
  color: #94a3b8;
  font-size: 12px;
}

.tool-del-btn:hover {
  color: var(--danger);
  background: #fee2e2;
}

.suboptions-bar {
  background: var(--teal-light);
  border: 1px solid var(--teal-border);
  border-radius: var(--radius);
  padding: 7px 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 8px;
  max-height: 155px;
  overflow-y: auto;
  flex-shrink: 0;
}

.subopt-group-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
}

.subopt-group-lbl {
  font-size: 11px;
  font-weight: 800;
  color: var(--teal-dark);
  margin-right: 4px;
  white-space: nowrap;
}

.subopt-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.subopt-chip {
  padding: 4px 9px;
  border-radius: 7px;
  border: 1px solid #cbd5e1;
  background: var(--surface);
  font-size: 11.5px;
  font-weight: 600;
  min-height: 28px;
}

.subopt-chip.active {
  background: var(--teal);
  color: #ffffff;
  border-color: var(--teal);
}

/* Canvas Area */
.canvas-area {
  position: relative;
  background: #e2e8f0;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 10px;
}

.active-tool-hint {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.80);
  color: #ffffff;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  z-index: 5;
  pointer-events: none;
  max-width: calc(100% - 24px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.canvas-viewport {
  position: relative;
  flex: 1;
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid #cbd5e1;
  box-shadow: var(--shadow-md);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  user-select: none;
}

#perio-chart-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* Interactive SVG hover highlights */
.hit-tooth {
  cursor: pointer;
}
.hit-tooth:hover {
  fill: rgba(15, 118, 110, 0.06);
}
.hit-papilla, .hit-lineangle, .hit-distal, .hit-regen {
  cursor: pointer;
}
.hit-papilla:hover, .hit-lineangle:hover, .hit-distal:hover, .hit-regen:hover {
  fill: rgba(15, 118, 110, 0.22);
}

/* Right Panel Controls */
.meta-fields, .form-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meta-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.meta-row-3 {
  display: grid;
  grid-template-columns: 76px 1fr 1fr;
  gap: 8px;
}

input[type="text"], input[type="date"], select {
  width: 100%;
  min-height: 36px;
  padding: 6px 9px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  background: var(--surface);
  font-family: inherit;
  font-size: 12.5px;
  color: var(--text);
}

input[type="color"] {
  width: 100%;
  height: 36px;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  padding: 2px;
  background: var(--surface);
  cursor: pointer;
}

.lang-toggle {
  display: flex;
  gap: 2px;
  background: var(--border);
  padding: 2px;
  border-radius: 7px;
}

.lang-btn {
  padding: 3px 8px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
  color: #475569;
}

.lang-btn.active {
  background: var(--surface);
  color: var(--teal);
}

.note-box {
  margin: 0;
  padding: 9px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Noto Sans TC", monospace;
  font-size: 11.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 230px;
  overflow-y: auto;
  color: #1e293b;
}

.legend-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.legend-swatch {
  width: 18px;
  height: 4px;
  border-radius: 2px;
  flex-shrink: 0;
}

.muted-text {
  font-size: 12px;
  color: var(--text-muted);
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.52);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 90dvh;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-card.modal-lg {
  max-width: 820px;
}

.modal-card.modal-sm {
  max-width: 380px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
}

.close-x {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 16px;
  color: var(--text-muted);
}

.close-x:hover {
  background: var(--bg);
  color: var(--text);
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: #334155;
  margin-bottom: 4px;
}

.export-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
}

.export-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.checkbox-stack {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 6px 0;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  cursor: pointer;
}

.export-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 6px;
}

.export-preview-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 240px;
}

.preview-label {
  align-self: flex-start;
  font-size: 11.5px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 6px;
}

#export-preview-img {
  max-width: 100%;
  max-height: 420px;
  border-radius: 6px;
  box-shadow: var(--shadow-sm);
  background: #ffffff;
}

.cases-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cases-section h3 {
  margin: 0;
  font-size: 13.5px;
  font-weight: 800;
  color: var(--teal-dark);
}

.save-case-row {
  display: flex;
  gap: 8px;
}

.case-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.case-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 11px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
}

.case-info {
  min-width: 0;
}

.case-title {
  font-size: 13px;
  font-weight: 700;
}

.case-desc {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 2px;
}

.case-btns {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.json-row {
  flex-direction: row;
  flex-wrap: wrap;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 72px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 90;
  pointer-events: none;
}

.toast {
  background: rgba(15, 23, 42, 0.90);
  color: #ffffff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  box-shadow: var(--shadow-md);
}

/* Mobile Responsive (< 960px) */
.mobile-only {
  display: none;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.42);
  z-index: 35;
}

@media (max-width: 959px) {
  .mobile-only {
    display: flex;
  }

  .topbar {
    padding: 6px 10px;
  }

  .brand-sub, .topbar .btn-lbl {
    display: none;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .canvas-area {
    padding: 6px;
  }

  .active-tool-hint {
    top: 10px;
    font-size: 11px;
    padding: 4px 10px;
  }

  .tool-sidebar, .right-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 58px;
    max-height: 74dvh;
    border-radius: 16px 16px 0 0;
    border: none;
    box-shadow: 0 -8px 28px rgba(15, 23, 42, 0.18);
    z-index: 40;
    transform: translateY(115%);
    transition: transform 0.22s ease;
  }

  .tool-sidebar.open, .right-panel.open {
    transform: translateY(0);
  }

  .sidebar-header {
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
  }

  .mobile-dock {
    display: grid;
    grid-template-columns: 1.5fr 1fr 1fr 1fr;
    gap: 6px;
    padding: 6px 10px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 30;
    flex-shrink: 0;
  }

  .dock-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-alt);
    font-size: 12px;
    font-weight: 700;
    padding: 0 8px;
  }

  .dock-btn.primary {
    background: var(--teal);
    color: #ffffff;
    border-color: var(--teal);
  }

  .export-grid {
    grid-template-columns: 1fr;
  }
}

/* Install / Share APP Modal */
.install-status-banner {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
}

.install-status-banner.ready {
  background: var(--teal-light);
  border-color: var(--teal-border);
  color: var(--teal-dark);
  font-weight: 600;
}

.install-url-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.install-url-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.install-url-row input {
  flex: 1;
  min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  background: var(--surface-alt);
}

.install-page-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--teal);
  text-decoration: none;
  margin-top: 2px;
}

.install-page-link:hover {
  text-decoration: underline;
  color: var(--teal-dark);
}

.install-steps-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.install-step-card {
  padding: 10px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 12px;
  line-height: 1.45;
  color: #334155;
}

.install-step-card strong {
  display: block;
  color: var(--teal-dark);
  margin-bottom: 4px;
  font-size: 12.5px;
}

@media (max-width: 720px) {
  .install-steps-grid {
    grid-template-columns: 1fr;
  }
}
