:root {
    --bg: #f4f1ea;
    --bg-alt: #ebe6db;
    --ink: #1a1f1a;
    --ink-soft: #4a4f47;
    --ink-muted: #8a8b82;
    --line: #d8d2c4;
    --line-soft: #e4dfd1;
    --accent: #a63d2a;
    --accent-soft: #c75a43;
    --surface: #fbf9f2;
    --surface-2: #ffffff;
    --shadow-sm: 0 1px 2px rgba(26, 31, 26, 0.04), 0 1px 1px rgba(26, 31, 26, 0.02);
    --shadow-md: 0 4px 24px rgba(26, 31, 26, 0.06), 0 1px 2px rgba(26, 31, 26, 0.04);
    --shadow-lg: 0 20px 60px rgba(26, 31, 26, 0.12), 0 2px 8px rgba(26, 31, 26, 0.06);
    --serif: 'Fraunces', Georgia, serif;
    --sans: 'Inter Tight', system-ui, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, monospace;
  }

  * { box-sizing: border-box; margin: 0; padding: 0; }

  html, body {
    font-family: var(--sans);
    color: var(--ink);
    background: var(--bg);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  body {
    min-height: 100vh;
    background-image:
      radial-gradient(circle at 20% 10%, rgba(166, 61, 42, 0.04), transparent 40%),
      radial-gradient(circle at 80% 90%, rgba(166, 61, 42, 0.03), transparent 40%);
  }

  /* ----------- Screen switcher ---------- */
  .screen { display: none; min-height: 100vh; }
  .screen.active { display: block; }

  /* =================================================
     LOGIN SCREEN
     ================================================= */
  #login-screen {
    display: none;
    min-height: 100vh;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    position: relative;
  }
  #login-screen.active { display: flex; }

  .login-grain {
    position: fixed;
    inset: 0;
    pointer-events: none;
    opacity: 0.4;
    mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.08 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  }

  .login-card {
    width: 100%;
    max-width: 440px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 2px;
    padding: 3.5rem 3rem;
    box-shadow: var(--shadow-lg);
    position: relative;
    z-index: 1;
  }

  .brand-mark {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 2.5rem;
  }

  .brand-dot {
    width: 10px;
    height: 10px;
    background: var(--accent);
    border-radius: 50%;
    position: relative;
  }
  .brand-dot::after {
    content: '';
    position: absolute;
    inset: -6px;
    border: 1px solid var(--accent);
    border-radius: 50%;
    opacity: 0.3;
  }

  .brand-name {
    font-family: var(--serif);
    font-weight: 400;
    font-size: 1.25rem;
    letter-spacing: 0.02em;
  }

  .login-title {
    font-family: var(--serif);
    font-size: 2.5rem;
    font-weight: 300;
    line-height: 1.1;
    margin-bottom: 0.5rem;
    letter-spacing: -0.02em;
  }
  .login-title em {
    font-style: italic;
    color: var(--accent);
  }

  .login-subtitle {
    color: var(--ink-muted);
    font-size: 0.875rem;
    margin-bottom: 2.5rem;
    font-family: var(--mono);
    text-transform: uppercase;
    letter-spacing: 0.12em;
  }

  .field {
    margin-bottom: 1.5rem;
  }

  .field label {
    display: block;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--ink-soft);
    margin-bottom: 0.5rem;
    font-weight: 500;
  }

  .field input,
  .field textarea {
    width: 100%;
    padding: 0.75rem 0;
    border: none;
    border-bottom: 1px solid var(--line);
    background: transparent;
    font-family: var(--sans);
    font-size: 1rem;
    color: var(--ink);
    outline: none;
    transition: border-color 0.2s;
  }

  .field input:focus,
  .field textarea:focus {
    border-bottom-color: var(--accent);
  }

  .btn {
    font-family: var(--sans);
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.85rem 1.5rem;
    border: 1px solid var(--ink);
    background: var(--ink);
    color: var(--bg);
    cursor: pointer;
    border-radius: 2px;
    letter-spacing: 0.02em;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
  }

  .btn:hover { background: var(--accent); border-color: var(--accent); }
  .btn.full { width: 100%; justify-content: center; }

  .btn-ghost {
    background: transparent;
    color: var(--ink);
    border: 1px solid var(--line);
  }
  .btn-ghost:hover { background: var(--ink); color: var(--bg); border-color: var(--ink); }

  .btn-subtle {
    background: transparent;
    color: var(--ink-soft);
    border: none;
    padding: 0.5rem 0.75rem;
    font-size: 0.8rem;
  }
  .btn-subtle:hover { color: var(--accent); background: transparent; }

  .login-meta {
    margin-top: 2rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--line-soft);
    font-family: var(--mono);
    font-size: 0.7rem;
    color: var(--ink-muted);
    letter-spacing: 0.08em;
    display: flex;
    justify-content: space-between;
  }

  /* =================================================
     APP LAYOUT
     ================================================= */
  .app-shell {
    display: grid;
    grid-template-columns: 340px 1fr;
    min-height: 100vh;
  }

  /* ---------- Sidebar ---------- */
  .sidebar {
    background: var(--surface);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: sticky;
    top: 0;
  }

  .sidebar-top {
    padding: 1.5rem 1.5rem 1rem;
    border-bottom: 1px solid var(--line-soft);
  }

  .sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.25rem;
  }

  .user-chip {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.8rem;
  }

  .avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 500;
    font-size: 0.75rem;
    font-family: var(--mono);
  }

  .search-wrap {
    position: relative;
  }

  .search-wrap input {
    width: 100%;
    padding: 0.65rem 0.75rem 0.65rem 2.25rem;
    border: 1px solid var(--line);
    background: var(--bg);
    border-radius: 2px;
    font-size: 0.875rem;
    font-family: var(--sans);
    outline: none;
    transition: border-color 0.2s;
  }
  .search-wrap input:focus { border-color: var(--accent); }

  .search-wrap svg {
    position: absolute;
    left: 0.75rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--ink-muted);
  }

  .sidebar-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 1rem;
    font-family: var(--mono);
    font-size: 0.7rem;
    color: var(--ink-muted);
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .patient-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
  }

  .patient-item {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--line-soft);
    cursor: pointer;
    transition: background 0.15s;
    position: relative;
  }
  .patient-item:hover { background: var(--bg-alt); }
  .patient-item.active {
    background: var(--bg-alt);
  }
  .patient-item.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--accent);
  }

  .patient-name {
    font-family: var(--serif);
    font-size: 1.05rem;
    font-weight: 500;
    letter-spacing: -0.01em;
    margin-bottom: 0.25rem;
  }

  .patient-meta {
    font-size: 0.78rem;
    color: var(--ink-muted);
    font-family: var(--mono);
    letter-spacing: 0.02em;
    display: flex;
    justify-content: space-between;
  }

  .sidebar-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--line-soft);
    display: flex;
    gap: 0.5rem;
  }

  .sidebar-footer .btn {
    flex: 1;
    padding: 0.6rem;
    font-size: 0.8rem;
    justify-content: center;
  }

  /* ---------- Main content ---------- */
  .main {
    padding: 2rem 2.5rem 4rem;
    overflow-y: auto;
  }

  .topbar {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 2rem;
    padding-bottom: 1.25rem;
    border-bottom: 1px solid var(--line);
  }

  .crumbs {
    font-family: var(--mono);
    font-size: 0.72rem;
    color: var(--ink-muted);
    letter-spacing: 0.12em;
    text-transform: uppercase;
    margin-bottom: 0.4rem;
  }
  .crumbs span { color: var(--accent); }

  .patient-title {
    font-family: var(--serif);
    font-size: 2.25rem;
    font-weight: 400;
    letter-spacing: -0.02em;
    line-height: 1;
  }

  .topbar-actions { display: flex; gap: 0.5rem; }

  .content-grid {
    display: grid;
    grid-template-columns: 1.1fr 1fr;
    gap: 2rem;
  }

  /* ---------- Sections ---------- */
  .section {
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-radius: 2px;
    padding: 1.5rem;
  }

  .section-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 1.25rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--line-soft);
  }

  .section-title {
    font-family: var(--serif);
    font-size: 1.15rem;
    font-weight: 500;
    letter-spacing: -0.01em;
  }

  .section-tag {
    font-family: var(--mono);
    font-size: 0.68rem;
    color: var(--ink-muted);
    text-transform: uppercase;
    letter-spacing: 0.1em;
  }

  .data-row {
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 1rem;
    padding: 0.85rem 0;
    border-bottom: 1px dashed var(--line-soft);
    align-items: start;
  }
  .data-row:last-child { border-bottom: none; }

  .data-label {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--ink-muted);
    font-weight: 500;
    padding-top: 0.2rem;
  }

  .data-value {
    font-size: 0.95rem;
    color: var(--ink);
  }

  .data-value input,
  .data-value textarea {
    width: 100%;
    padding: 0.45rem 0.6rem;
    border: 1px solid transparent;
    background: transparent;
    font-family: var(--sans);
    font-size: 0.95rem;
    color: var(--ink);
    outline: none;
    border-radius: 2px;
    transition: all 0.15s;
    margin: -0.45rem -0.6rem;
  }

  .data-value input:hover,
  .data-value textarea:hover {
    background: var(--bg);
  }

  .data-value input:focus,
  .data-value textarea:focus {
    border-color: var(--accent);
    background: var(--surface);
  }

  .comments-area {
    width: 100%;
    min-height: 180px;
    padding: 1rem;
    border: 1px solid var(--line);
    background: var(--bg);
    font-family: var(--mono);
    font-size: 0.85rem;
    line-height: 1.65;
    color: var(--ink);
    outline: none;
    border-radius: 2px;
    resize: vertical;
    white-space: pre-wrap;
    transition: border-color 0.2s;
  }
  .comments-area:focus { border-color: var(--accent); background: var(--surface); }

  .comments-meta {
    display: flex;
    justify-content: space-between;
    margin-top: 0.5rem;
    font-family: var(--mono);
    font-size: 0.7rem;
    color: var(--ink-muted);
    letter-spacing: 0.05em;
  }

  /* ---------- Anatomy / canvas section ---------- */
  .anatomy-section {
    grid-column: 1 / -1;
    background: var(--surface-2);
    border: 1px solid var(--line);
    padding: 1.5rem;
  }

  .anatomy-wrap {
    display: grid;
    grid-template-columns: 1fr 240px;
    gap: 2rem;
    align-items: start;
  }

  .canvas-stack {
    position: relative;
    width: 100%;
    aspect-ratio: 3 / 4;
    background:
      linear-gradient(var(--line-soft) 1px, transparent 1px),
      linear-gradient(90deg, var(--line-soft) 1px, transparent 1px),
      var(--bg);
    background-size: 24px 24px, 24px 24px, 100% 100%;
    border: 1px solid var(--line);
    border-radius: 2px;
    overflow: hidden;
    cursor: crosshair;
  }

  .canvas-stack svg.anatomy {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
  }

  .canvas-stack canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    touch-action: none;
  }

  .canvas-label {
    position: absolute;
    top: 0.75rem;
    left: 0.75rem;
    font-family: var(--mono);
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--ink-muted);
    background: var(--bg);
    padding: 0.25rem 0.5rem;
    border: 1px solid var(--line);
  }

  .canvas-corner {
    position: absolute;
    font-family: var(--mono);
    font-size: 0.6rem;
    color: var(--ink-muted);
    letter-spacing: 0.1em;
  }
  .canvas-corner.tr { top: 0.75rem; right: 0.75rem; }
  .canvas-corner.bl { bottom: 0.75rem; left: 0.75rem; }
  .canvas-corner.br { bottom: 0.75rem; right: 0.75rem; }

  /* ---------- Tool panel ---------- */
  .tools {
    background: var(--bg);
    border: 1px solid var(--line);
    padding: 1.25rem;
    border-radius: 2px;
  }

  .tool-group {
    margin-bottom: 1.5rem;
  }
  .tool-group:last-child { margin-bottom: 0; }

  .tool-label {
    font-family: var(--mono);
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--ink-muted);
    margin-bottom: 0.5rem;
  }

  .color-row {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 0.4rem;
  }

  .swatch {
    aspect-ratio: 1;
    border: 2px solid transparent;
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    transition: transform 0.15s;
  }
  .swatch:hover { transform: scale(1.1); }
  .swatch.active {
    border-color: var(--ink);
    transform: scale(1.1);
  }

  .brush-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
  }

  .brush-btn {
    flex: 1;
    padding: 0.5rem;
    border: 1px solid var(--line);
    background: var(--surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
  }
  .brush-btn:hover { border-color: var(--ink-soft); }
  .brush-btn.active { border-color: var(--accent); background: var(--surface); }

  .brush-dot {
    background: currentColor;
    border-radius: 50%;
    color: var(--ink);
  }

  .tool-actions {
    display: grid;
    gap: 0.5rem;
  }

  .tool-actions .btn {
    justify-content: center;
    padding: 0.6rem;
    font-size: 0.8rem;
  }

  .status-line {
    display: flex;
    justify-content: space-between;
    margin-top: 1rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--line-soft);
    font-family: var(--mono);
    font-size: 0.68rem;
    color: var(--ink-muted);
    letter-spacing: 0.05em;
  }

  .status-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #4a9b5f;
    margin-right: 0.35rem;
    vertical-align: middle;
  }

  /* ---------- Tag / chip ---------- */
  .chip {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    background: var(--bg-alt);
    border: 1px solid var(--line);
    font-family: var(--mono);
    font-size: 0.7rem;
    letter-spacing: 0.05em;
    border-radius: 2px;
    margin-right: 0.3rem;
  }

  /* ---------- Empty state small ---------- */
  .toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--ink);
    color: var(--bg);
    padding: 0.75rem 1.25rem;
    border-radius: 2px;
    font-size: 0.85rem;
    font-family: var(--mono);
    letter-spacing: 0.05em;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateY(10px);
    transition: all 0.3s;
    pointer-events: none;
    z-index: 100;
  }
  .toast.show { opacity: 1; transform: translateY(0); }

  @media (max-width: 960px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { height: auto; position: static; }
    .content-grid { grid-template-columns: 1fr; }
    .anatomy-wrap { grid-template-columns: 1fr; }
  }

/* ---------- Root Informatics footer ---------- */
.ri-footer {
  position: fixed;
  bottom: 0.75rem;
  right: 1rem;
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--ink-muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  z-index: 50;
  opacity: 0.7;
  transition: opacity 0.2s;
}
.ri-footer:hover { opacity: 1; }
.ri-footer a { color: var(--ink-soft); text-decoration: none; border-bottom: 1px solid var(--line); }
.ri-footer a:hover { color: var(--accent); border-color: var(--accent); }
