:root {
    --bg: #f6f8f8;
    --surface: #ffffff;
    --surface-soft: #eef7f6;
    --text: #1f2a2e;
    --muted: #647177;
    --line: #dce5e7;
    --accent: #16877f;
    --accent-dark: #0f625d;
    --danger: #b42318;
    --success: #147a3f;
    --shadow: 0 18px 40px rgba(20, 46, 51, 0.08);
    --radius: 8px;
}

:root[data-theme="dark"] {
    --bg: #111719;
    --surface: #182124;
    --surface-soft: #203234;
    --text: #edf5f5;
    --muted: #9fb0b5;
    --line: #2d3b40;
    --accent: #35b9ae;
    --accent-dark: #77ddd5;
    --danger: #ff8a7f;
    --success: #61d394;
    --shadow: 0 18px 40px rgba(0, 0, 0, 0.22);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.5;
}

a {
    color: var(--accent-dark);
    text-decoration: none;
}

button,
input,
textarea {
    font: inherit;
}

input,
textarea,
select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface);
    color: var(--text);
    padding: 11px 12px;
    outline: none;
}

textarea {
    resize: vertical;
}

input:focus,
textarea:focus,
select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(22, 135, 127, 0.14);
}

label {
    display: grid;
    gap: 7px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 650;
}

.field-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.mic-button {
    width: 30px;
    height: 30px;
    display: inline-grid;
    place-items: center;
    flex: 0 0 auto;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface-soft);
    color: var(--accent-dark);
    cursor: pointer;
    padding: 0;
}

.mic-button svg {
    width: 16px;
    height: 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.mic-button.recording {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    border-bottom: 1px solid var(--line);
    padding: 13px 10px;
    text-align: left;
    vertical-align: top;
}

th {
    color: var(--muted);
    font-size: 12px;
    letter-spacing: 0;
    text-transform: uppercase;
}

.main-header {
    min-height: 68px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 12px 32px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand,
.header-tools {
    display: flex;
    align-items: center;
    gap: 14px;
}

.brand {
    color: var(--text);
    font-weight: 800;
}

.header-tools {
    margin-left: auto;
}

.usage-pill {
    min-width: 104px;
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0 8px;
    align-items: baseline;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface-soft);
    padding: 8px 11px;
    color: var(--muted);
    cursor: default;
}

.usage-pill span {
    font-size: 12px;
    font-weight: 750;
}

.usage-pill strong {
    color: var(--text);
    font-size: 14px;
    text-align: right;
}

.theme-toggle {
    width: 74px;
    height: 38px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
    padding: 3px;
}

.logout-button {
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    font-weight: 750;
    padding: 8px 13px;
}

.theme-toggle span {
    display: grid;
    place-items: center;
    height: 30px;
    border-radius: 999px;
    font-size: 16px;
}

:root:not([data-theme="dark"]) [data-theme-sun],
:root[data-theme="dark"] [data-theme-moon] {
    background: var(--accent);
    color: #fff;
}

.brand-mark {
    width: 36px;
    height: 36px;
    display: inline-grid;
    place-items: center;
    border-radius: 8px;
    background: var(--surface-soft);
    border: 1px solid var(--line);
    padding: 6px;
}

.brand-mark img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.brand-mark.large {
    width: 58px;
    height: 58px;
    border-radius: 14px;
    padding: 9px;
}

.app-shell {
    width: min(1220px, calc(100% - 32px));
    margin: 28px auto 56px;
}

.app-shell.narrow {
    width: min(820px, calc(100% - 32px));
}

.top-grid,
.content-grid,
.consultation-layout,
.usage-grid {
    display: grid;
    gap: 18px;
    min-width: 0;
}

.top-grid {
    grid-template-columns: 180px 180px 1fr;
    margin-bottom: 18px;
}

.usage-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 22px;
}

.content-grid {
    grid-template-columns: minmax(0, 1fr) 340px;
    align-items: start;
}

.consultation-layout {
    grid-template-columns: minmax(0, 1fr) 390px;
    align-items: start;
}

.consultation-layout > *,
.content-grid > *,
.top-grid > *,
.usage-grid > * {
    min-width: 0;
}

.panel,
.metric,
.notice,
.patient-hero,
.login-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.panel {
    padding: 22px;
    min-width: 0;
}

.panel-head,
.patient-hero,
.actions,
.search-row,
.dictation-box {
    display: flex;
    align-items: center;
    gap: 12px;
}

.panel-head,
.patient-hero {
    justify-content: space-between;
    margin-bottom: 18px;
    flex-wrap: wrap;
}

h1,
h2,
h3,
p {
    margin-top: 0;
}

h1 {
    margin-bottom: 4px;
    font-size: clamp(26px, 4vw, 36px);
    line-height: 1.12;
}

h2 {
    margin-bottom: 14px;
    font-size: 20px;
}

h3 {
    margin-bottom: 4px;
    font-size: 16px;
}

.panel-head p,
.patient-hero p,
.empty,
.notice,
.timeline-item small,
.date,
.draft-warning,
.back-link {
    color: var(--muted);
}

.metric {
    padding: 18px;
}

.metric span {
    display: block;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.metric strong {
    display: block;
    margin-top: 4px;
    font-size: 34px;
    line-height: 1;
}

.metric small {
    display: block;
    margin-top: 8px;
    color: var(--muted);
    font-weight: 700;
}

.notice {
    display: flex;
    align-items: center;
    padding: 18px 20px;
    background: var(--surface-soft);
    color: #265956;
    box-shadow: none;
}

:root[data-theme="dark"] .notice {
    color: var(--text);
}

.notification-entry {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 2px 14px;
    color: var(--text);
}

.notification-entry span,
.notification-entry small {
    color: var(--muted);
    font-weight: 750;
}

.notification-entry strong {
    grid-row: span 2;
    align-self: center;
    font-size: 34px;
    line-height: 1;
}

.notification-list {
    display: grid;
    gap: 12px;
}

.notification-card {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 14px;
    align-items: center;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfdfd;
}

:root[data-theme="dark"] .notification-card {
    background: #151d20;
}

.notification-card h2 {
    margin: 3px 0 4px;
}

.notification-card p,
.notification-card small {
    display: block;
    margin: 0;
    color: var(--muted);
}

.sent-status {
    display: block;
    margin-top: 9px;
    color: var(--success);
    font-size: 13px;
}

.patient-hero {
    padding: 24px;
    margin-bottom: 18px;
}

.search-row {
    margin-bottom: 12px;
}

.search-row input {
    flex: 1;
}

.primary-button,
.secondary-button,
.ghost-button,
.search-row button {
    min-height: 42px;
    border: 1px solid transparent;
    border-radius: 7px;
    padding: 10px 14px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 750;
    white-space: nowrap;
}

.primary-button {
    background: var(--accent);
    color: #fff;
}

.primary-button:hover {
    background: var(--accent-dark);
}

.secondary-button,
.search-row button {
    background: #e4f3f1;
    border-color: #b9dcda;
    color: var(--accent-dark);
}

:root[data-theme="dark"] .secondary-button,
:root[data-theme="dark"] .search-row button {
    background: #183c3b;
    border-color: #2c6966;
    color: var(--accent-dark);
}

.ghost-button {
    background: var(--surface);
    border-color: var(--line);
    color: var(--text);
}

.danger-button {
    min-height: 42px;
    border: 1px solid #ffb4ad;
    border-radius: 7px;
    background: #fff4f2;
    color: var(--danger);
    cursor: pointer;
    font-size: 14px;
    font-weight: 800;
    padding: 10px 14px;
}

.danger-button.compact {
    min-height: 36px;
    padding: 8px 10px;
}

:root[data-theme="dark"] .danger-button {
    background: #3a1d1a;
    border-color: #6e332d;
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.recording {
    background: #b42318;
    color: #fff;
}

.table-wrap {
    overflow-x: auto;
}

.side-panel {
    position: sticky;
    top: 88px;
}

.timeline,
.consultation-list,
.stack {
    display: grid;
    gap: 12px;
}

.timeline-item,
.consultation-card {
    display: grid;
    gap: 4px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #fbfdfd;
}

:root[data-theme="dark"] .timeline-item,
:root[data-theme="dark"] .consultation-card {
    background: #151d20;
}

.consultation-card {
    grid-template-columns: 1fr auto;
    align-items: center;
}

.xray-gallery {
    position: relative;
}

.file-list {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(220px, 280px);
    gap: 14px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scrollbar-width: thin;
    padding: 4px 4px 12px;
}

.file-card {
    display: grid;
    grid-template-rows: 170px auto auto;
    gap: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fbfdfd;
    padding: 12px;
    scroll-snap-align: start;
}

:root[data-theme="dark"] .file-card {
    background: #151d20;
}

.file-thumb {
    display: grid;
    place-items: center;
    min-width: 0;
    padding: 8px;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #eef5f4;
}

.file-thumb img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 5px;
}

:root[data-theme="dark"] .file-thumb {
    background: #0c171a;
}

.pdf-thumb {
    display: grid;
    place-items: center;
    width: 72px;
    height: 88px;
    border-radius: 7px;
    background: var(--surface);
    border: 1px solid var(--line);
    color: var(--danger);
    font-weight: 900;
}

.file-meta {
    min-width: 0;
}

.file-meta strong {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-card small {
    display: block;
    color: var(--muted);
    font-weight: 700;
    margin-top: 3px;
}

.file-card p {
    margin: 8px 0 0;
    color: var(--muted);
}

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

.gallery-nav {
    position: absolute;
    top: 82px;
    z-index: 2;
    width: 36px;
    height: 36px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
    cursor: pointer;
    font-size: 25px;
    line-height: 1;
}

.gallery-nav.prev {
    left: -8px;
}

.gallery-nav.next {
    right: -8px;
}

.viewer-open {
    overflow: hidden;
}

.image-viewer[hidden] {
    display: none;
}

.image-viewer {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    background: rgba(5, 12, 15, 0.86);
    padding: 24px;
}

.image-viewer figure {
    width: min(100%, 980px);
    max-height: min(90vh, 860px);
    display: grid;
    gap: 12px;
    margin: 0;
}

.image-viewer img,
.image-viewer iframe {
    max-width: 100%;
    max-height: calc(90vh - 70px);
    justify-self: center;
    border-radius: 8px;
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
}

.image-viewer img {
    object-fit: contain;
    background: #000;
}

.image-viewer iframe {
    width: min(100%, 980px);
    height: calc(90vh - 70px);
    border: 0;
    background: #fff;
}

.image-viewer figcaption {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    color: #fff;
    font-size: 14px;
}

.image-viewer figcaption span {
    color: rgba(255, 255, 255, 0.72);
}

.image-viewer-close {
    position: fixed;
    top: 18px;
    right: 18px;
    width: 44px;
    height: 44px;
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    font-size: 32px;
    line-height: 1;
}

.patient-files-grid {
    margin-top: 18px;
}

.danger-zone {
    display: grid;
    gap: 12px;
    border-top: 1px solid var(--line);
    margin-top: 20px;
    padding-top: 20px;
}

.danger-zone p {
    color: var(--muted);
}

.date,
.timeline-item span {
    font-size: 13px;
    font-weight: 750;
}

.details {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 12px 16px;
}

.details dt {
    color: var(--muted);
    font-weight: 700;
}

.details dd {
    margin: 0;
}

.usage-details {
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    padding: 16px 0;
    margin: 0 0 20px;
}

.reset-box {
    display: grid;
    gap: 12px;
}

.reset-box p {
    color: var(--muted);
}

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

.span-2 {
    grid-column: span 2;
}

.draft-panel {
    display: grid;
    gap: 14px;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
}

.draft-panel label,
.draft-panel textarea,
textarea[name="drafted_note"] {
    min-width: 0;
    max-width: 100%;
}

.draft-panel textarea {
    display: block;
    overflow-wrap: break-word;
    word-break: normal;
}

.draft-panel .actions {
    flex-wrap: wrap;
    align-items: stretch;
}

.draft-panel .actions > * {
    flex: 1 1 140px;
}

.draft-panel .actions button,
.draft-panel .actions .print-format-control {
    min-height: 42px;
}

.draft-panel .actions .print-format-control {
    flex: 1 1 160px;
}

.draft-panel .actions .ghost-button {
    flex-grow: 0;
}

.draft-warning {
    border-left: 4px solid var(--accent);
    background: var(--surface-soft);
    padding: 10px 12px;
    font-size: 13px;
}

.draft-approved {
    border-left: 4px solid var(--success);
    background: #eefbf3;
    color: #116b38;
    padding: 10px 12px;
    font-size: 13px;
    font-weight: 700;
}

:root[data-theme="dark"] .draft-approved {
    background: #163424;
    color: #8ee6ad;
}

.status-badge {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    border-radius: 999px;
    padding: 7px 12px;
    font-size: 13px;
    font-weight: 800;
}

.status-badge.draft {
    background: #fff6e6;
    color: #8a5600;
}

.status-badge.approved {
    background: #e8f8ee;
    color: #116b38;
}

:root[data-theme="dark"] .status-badge.draft {
    background: #3a2b14;
    color: #ffd48a;
}

:root[data-theme="dark"] .status-badge.approved {
    background: #163424;
    color: #8ee6ad;
}

.alert {
    border-radius: 7px;
    padding: 12px 14px;
    margin-bottom: 16px;
    border: 1px solid var(--line);
}

.alert.danger {
    color: var(--danger);
    background: #fff4f2;
    border-color: #ffd4ce;
}

.alert.success {
    color: var(--success);
    background: #eefbf3;
    border-color: #c8ecd5;
}

.alert.subtle {
    display: none;
    margin: 0;
    color: #405057;
    background: var(--bg);
}

.print-format-control {
    min-width: 160px;
    font-size: 12px;
    margin: 0;
}

.print-format-control select {
    min-height: 42px;
    padding: 8px 10px;
}

.print-only {
    display: none;
}

.print-sheet {
    color: #111;
    background: #fff;
}

.print-sheet-head,
.print-patient,
.print-footer {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 20px;
}

.print-sheet-head {
    border-bottom: 2px solid #111;
    padding-bottom: 12px;
    margin-bottom: 16px;
}

.print-sheet-head div,
.print-footer {
    display: grid;
    gap: 4px;
}

.print-sheet-head span,
.print-footer {
    color: #444;
    font-size: 12px;
}

.print-patient {
    border-bottom: 1px solid #bbb;
    padding-bottom: 10px;
    margin-bottom: 14px;
}

.print-patient p,
.print-section p {
    margin: 0 0 8px;
    white-space: pre-wrap;
}

.print-section {
    margin-bottom: 14px;
}

.print-section h2 {
    margin-bottom: 6px;
    font-size: 15px;
    border-bottom: 1px solid #ddd;
    padding-bottom: 3px;
}

.print-footer {
    border-top: 1px solid #bbb;
    padding-top: 12px;
    margin-top: 22px;
}

.alert.subtle.visible,
.alert.subtle:not(:empty) {
    display: block;
}

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(180deg, #f7fbfb 0%, #edf4f4 100%);
}

.login-shell {
    width: min(430px, calc(100% - 32px));
}

.login-card {
    padding: 32px;
}

.login-card .brand-mark {
    margin-bottom: 18px;
}

@media (max-width: 920px) {
    .top-grid,
    .content-grid,
    .consultation-layout,
    .usage-grid {
        grid-template-columns: 1fr;
    }

    .side-panel {
        position: static;
    }
}

@media (max-width: 640px) {
    .main-header {
        height: auto;
        align-items: flex-start;
        flex-direction: column;
        padding: 16px;
    }

    .header-tools {
        width: 100%;
        margin-left: 0;
        justify-content: space-between;
    }

    .usage-pill {
        flex: 1;
    }

    .theme-toggle,
    .logout-button {
        flex: 0 0 auto;
    }

    .app-shell {
        width: min(100% - 20px, 1220px);
        margin-top: 16px;
    }

    .panel-head,
    .patient-hero,
    .actions,
    .dictation-box {
        align-items: stretch;
        flex-direction: column;
    }

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

    .span-2 {
        grid-column: span 1;
    }

    .draft-panel .actions {
        display: grid;
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .draft-panel .actions > *,
    .draft-panel .actions .print-format-control,
    .draft-panel .actions .ghost-button {
        width: 100%;
        min-height: 42px;
        flex: none;
    }

    .draft-panel .actions button {
        padding: 10px 12px;
    }

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

    .notification-card {
        grid-template-columns: 1fr;
        align-items: stretch;
    }

    .notification-card .primary-button {
        width: 100%;
    }

    .file-list {
        grid-auto-columns: minmax(245px, 86vw);
        padding-inline: 2px;
    }

    .gallery-nav {
        top: 74px;
        width: 34px;
        height: 34px;
        font-size: 23px;
        background: var(--surface);
        background: color-mix(in srgb, var(--surface) 88%, transparent);
        backdrop-filter: blur(8px);
        box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
    }

    .gallery-nav.prev {
        left: 4px;
    }

    .gallery-nav.next {
        right: 4px;
    }

    .image-viewer {
        padding: 14px;
    }

    .image-viewer-close {
        top: 10px;
        right: 10px;
    }

    .image-viewer figcaption {
        flex-direction: column;
        gap: 3px;
    }
}

@media print {
    body {
        background: #fff;
    }

    .main-header,
    .print-hidden,
    .panel-head .primary-button,
    .actions,
    .consultation-layout {
        display: none !important;
    }

    .app-shell,
    .print-sheet {
        width: 100%;
        margin: 0;
        display: block;
    }

    .panel {
        border: 0;
        box-shadow: none;
        padding: 0;
        margin-bottom: 18px;
    }

    input,
    textarea {
        border: 0;
        padding: 0;
        min-height: auto;
    }

    .print-only {
        display: block !important;
    }
}
