:root {
  color-scheme: light;
  font-family: "Segoe UI Variable", "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  color: #172d2b;
  background: #f4f7f6;
  --ink: #172d2b;
  --muted: #526763;
  --accent: #176c5a;
  --accent-dark: #115444;
  --accent-light: #e7f4ee;
  --line: #dbe6e1;
  --paper: #ffffff;
  --radius: 16px;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 300px; }
h1, h2, h3, h4, p { margin: 0; }
button, input, select, textarea { font: inherit; }
button, select, input[type="checkbox"], input[type="radio"], summary { cursor: pointer; }
input, textarea, select { color: var(--ink); }
button:disabled, input:disabled { cursor: default; }
a { color: var(--accent-dark); }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid #167864; outline-offset: 3px; }

.site-header {
  min-height: 73px;
  padding: 0.8rem max(1.35rem, calc((100% - 1480px) / 2));
  display: flex;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 0.8rem; color: var(--ink); text-decoration: none; }
.brand strong { display: block; line-height: 1.3; font-size: 1.06rem; }
.brand small { display: block; color: var(--muted); font-size: 0.8rem; }
.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 39px;
  height: 39px;
  background: var(--accent);
  border-radius: 12px;
  color: white;
  font-size: 1.8rem;
  line-height: 1;
}
.header-actions, .inline-actions, .review-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-wrap: wrap;
}
.button {
  border: 1px solid transparent;
  padding: 0.58rem 0.82rem;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 650;
  line-height: 1.3;
  text-align: center;
}
.button.primary { background: var(--accent); color: white; }
.button.primary:hover:enabled { background: var(--accent-dark); }
.button.secondary { border-color: var(--line); background: white; color: var(--ink); }
.button.secondary:hover:enabled { border-color: #92b5a8; background: #f4f9f7; }
.button.quiet { color: var(--accent-dark); background: transparent; }
.button.quiet:hover:enabled { background: var(--accent-light); }
.button.danger { color: #9a3727; }
.button.danger:hover:enabled { background: #feece5; }
.button:disabled { opacity: 0.49; }

#workspace { max-width: 1520px; margin: 0 auto; padding: 2.15rem 1.35rem 3rem; }
.workspace-heading { display: flex; justify-content: space-between; align-items: end; gap: 0.7rem 1.4rem; flex-wrap: wrap; margin-bottom: 1.55rem; }
.workspace-heading h1 { font-size: clamp(1.58rem, 3.4vw, 2.3rem); line-height: 1.16; font-weight: 680; letter-spacing: -0.043em; }
.workspace-heading p { margin-top: 0.4rem; color: var(--muted); }
.workspace-tools { display: flex; align-items: center; gap: 0.65rem 1rem; flex-wrap: wrap; }
.privacy { display: inline-flex; align-items: center; gap: 0.5rem; padding: 0.45rem 0.7rem; border-radius: 8px; background: var(--accent-light); color: var(--accent-dark); font-size: 0.8rem; font-weight: 600; }
.privacy::before { content: ''; display: block; width: 0.45rem; height: 0.45rem; border-radius: 100%; background: var(--accent); }
.mobile-jump { display: none; }
.workspace-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; gap: 1.45rem; }
.editor-fields { display: grid; gap: 0.95rem; border: 0; padding: 0; margin: 0; min-width: 0; }
.card { padding: 1.2rem; border: 1px solid var(--line); border-radius: var(--radius); background: white; box-shadow: 0 2px 4px #152d2405; min-width: 0; }
.card-heading { display: flex; gap: 0.68rem; align-items: center; margin-bottom: 1.1rem; }
.card-heading h2 { font-size: 1.14rem; letter-spacing: -0.022em; }
.card-heading p { color: var(--muted); font-size: 0.86rem; margin-top: 0.2rem; }
.section-icon { flex: 0 0 2.15rem; height: 2.15rem; display: grid; place-items: center; color: var(--accent-dark); background: var(--accent-light); border-radius: 9px; font-weight: 750; }
.field { display: flex; flex-direction: column; gap: 0.37rem; min-width: 0; margin: 0 0 0.75rem; font-size: 0.86rem; font-weight: 650; }
.field input, .field select, .field textarea {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 2.55rem;
  padding: 0.5rem 0.66rem;
  border: 1px solid #c8d8d1;
  border-radius: 8px;
  background: #fcfdfd;
  font-weight: 400;
  font-size: 1rem;
}
.field textarea { resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); background: white; }
.field :disabled { background: #f0f4f2; color: var(--muted); }
.field small, .field-note { color: var(--muted); font-size: 0.83rem; font-weight: 400; line-height: 1.5; }
.field-note { margin: 0.24rem 0 0.76rem; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 125px), 1fr)); gap: 0 0.65rem; }
.scheme-choice, .subcard { min-width: 0; }
.scheme-choice { border: 0; margin: 0 0 1rem; padding: 0; }
.scheme-choice legend { margin-bottom: 0.45rem; padding: 0; font-size: 0.86rem; font-weight: 650; }
.segmented { display: flex; gap: 0.25rem; padding: 0.26rem; border-radius: 10px; background: #eef3f0; }
.segmented label { position: relative; flex: 1 1 0; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; }
.segmented span { display: grid; place-items: center; min-height: 2.45rem; height: 100%; padding: 0.33rem; border: 1px solid transparent; border-radius: 8px; text-align: center; font-size: 0.86rem; font-weight: 650; pointer-events: none; }
.segmented input:checked + span { color: var(--accent-dark); border-color: #c1d9cf; background: white; box-shadow: 0 1px 2px #203a2520; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
.check { display: flex; align-items: start; gap: 0.48rem; margin: 0.48rem 0 0.65rem; font-size: 0.88rem; line-height: 1.45; }
.check input { flex: 0 0 auto; margin: 0.14rem 0 0; width: 1.02rem; height: 1.02rem; accent-color: var(--accent); }
.check:has(input:disabled) { color: var(--muted); }
.model-actions .button:first-child { margin-left: -0.64rem; }
.subheading { display: flex; flex-wrap: wrap; gap: 0.25rem 0.55rem; align-items: center; justify-content: space-between; margin: 0.9rem 0 0.66rem; padding-top: 0.72rem; border-top: 1px solid var(--line); }
.subheading h3 { font-size: 0.98rem; }
.subcard { margin: 0.2rem 0 0.85rem; padding: 0.66rem 0.72rem 0; border: 1px solid var(--line); border-radius: 10px; background: #f8fbfa; }
.subcard legend { padding: 0 0.36rem; font-weight: 650; font-size: 0.85rem; }
.confirm-row { display: flex; align-items: start; gap: 0.55rem; }
.confirm-row .field { flex: 1; margin-bottom: 0.23rem; }
.confirm-row > .button { margin-top: 1.55rem; }
.meal-options { display: flex; flex-wrap: wrap; gap: 0.1rem 0.8rem; }
.calculator { margin-top: 0.85rem; padding: 0.75rem; border: 1px solid var(--line); border-radius: 10px; background: #f8fbfa; }
.calculator summary { font-weight: 650; font-size: 0.9rem; }
.estimate-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); gap: 0.55rem; margin: 0.55rem 0; }
.estimate { padding: 0.65rem; background: white; border: 1px solid var(--line); border-radius: 8px; overflow-wrap: anywhere; }
.estimate small, .estimate strong { display: block; }
.estimate small { color: var(--muted); margin-bottom: 0.2rem; }
.estimate .button { margin-left: -0.6rem; }
.teaching-card { border-left: 3px solid #80a99b; border-radius: 5px; background: #f2f8f5; padding: 0.6rem 0.7rem; margin: 0.35rem 0 0.7rem; }
.teaching-card strong, .teaching-card small { display: block; }
.teaching-card small { color: var(--muted); margin-top: 0.15rem; }

.preview-pane {
  position: sticky;
  top: 1rem;
  min-width: 0;
  max-height: calc(100dvh - 2rem);
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.95rem;
  background: #e9efec;
  border: 1px solid #d8e4de;
  border-radius: var(--radius);
}
.preview-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 0.75rem; padding: 0 0.1rem; }
.preview-toolbar h2 { font-size: 1.02rem; }
.preview-toolbar p { color: var(--muted); font-size: 0.8rem; margin-top: 0.18rem; }
.status { padding: 0.32rem 0.6rem; border: 1px solid #e3d6ae; border-radius: 20px; background: #fcf4d7; color: #624818; font-size: 0.8rem; font-weight: 700; }
.status.ready { background: #d7eee3; color: var(--accent-dark); border-color: #9fcfba; }
.paper-frame { overflow: auto; border: 1px solid #dbe5df; background: white; border-radius: 9px; box-shadow: 0 2px 8px #1b3a2a09; }
#paper { padding: clamp(0.9rem, 2.25vw, 2.25rem); font-family: Arial, Helvetica, sans-serif; line-height: 1.47; color: #172421; overflow-wrap: anywhere; min-height: 330px; }
.paper-heading { padding-bottom: 0.68rem; border-bottom: 2px solid #235949; }
.paper-kicker { margin-bottom: 0.3rem; color: #385e51; font-size: 0.73em; font-weight: bold; letter-spacing: 0.08em; }
.paper-heading h2 { font-size: 1.69em; line-height: 1.13; letter-spacing: -0.027em; }
.paper-meta { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 0.18rem 0.8rem; margin-top: 0.55rem; font-size: 0.91em; }
.paper-section { margin-top: 1.02em; }
.paper-section h3 { margin-bottom: 0.2em; color: #184e40; font-size: 1.05em; line-height: 1.3; }
.paper-section h4 { margin: 0.45em 0 0.18em; font-size: 1em; }
.paper-section p, .paper-section ul { margin: 0.26em 0; }
.paper-section ul { padding-left: 1.25em; }
.paper-section li + li { margin-top: 0.17em; }
.paper-section .insulin-name { font-weight: bold; font-size: 1.19em; line-height: 1.26; margin: 0.2em 0; }
.insulin-name small { font-size: 0.78em; font-weight: normal; }
.dose-summary { font-size: 1.09em; font-weight: bold; }
.draft-banner { padding: 0.35em 0.55em; margin: 0.64em 0; background: #fcf4d7; border-radius: 5px; color: #624818; font-size: 0.83em; font-weight: bold; }
.paper-placeholder { padding: 0.45em 0.65em; background: #f3f6f4; border-radius: 5px; color: var(--muted); }
.table-scroll { overflow-x: auto; margin: 0.65em 0 0.3em; }
.dose-table { width: 100%; border-collapse: collapse; font-size: 0.94em; line-height: 1.3; }
.dose-table caption { text-align: left; font-weight: bold; font-size: 1.04em; padding-bottom: 0.3em; }
.dose-table th, .dose-table td { border: 1px solid #8a9b93; padding: 0.34em 0.4em; text-align: center; vertical-align: middle; }
.dose-table th:first-child { text-align: left; }
.dose-table th { background: #ecf4f0; }
.dose-table td small { display: block; font-size: 0.9em; }
.dose-table .no-dose { font-weight: bold; }
.dose-table .dose-note td { text-align: left; white-space: pre-line; }
.dose-table[data-meals="4"], .dose-table[data-meals="5"] { min-width: 430px; }
.table-key { font-size: 0.87em; }
.teaching + .teaching { border-top: 1px solid var(--line); padding-top: 0.7em; }
.teaching p { margin-top: 0.45em; }
.preserve-lines { white-space: pre-line; }
.review-card { background: white; border: 1px solid var(--line); border-radius: 9px; padding: 0.65rem 0.77rem; }
.review-card h3 { font-size: 0.93rem; margin-bottom: 0.33rem; }
.review-card .check { margin-block: 0.34rem 0.55rem; }
.issue-list { color: #82501e; font-size: 0.85rem; }
.issue-list ul { max-height: 6.3rem; overflow: auto; padding-left: 1.1rem; margin-block: 0.34rem; }
.issue-list li + li { margin-top: 0.2rem; }
.issue-list button { border: 0; background: none; color: #724418; text-decoration: underline; text-align: left; padding: 0; }
#print-blocked { display: none; }

dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(640px, calc(100% - 2rem)); max-height: calc(100dvh - 2rem); color: var(--ink); background: white; box-shadow: 0 22px 60px #14282138; }
dialog::backdrop { background: #102d2680; }
#template-editor { width: min(970px, calc(100% - 2rem)); }
#confirm-dialog { width: min(480px, calc(100% - 2rem)); }
.dialog-heading, .dialog-footer { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 0.45rem; padding: 0.86rem 1.13rem; }
.dialog-heading { border-bottom: 1px solid var(--line); }
.dialog-heading h2 { font-size: 1.2rem; }
.dialog-heading p { color: var(--muted); font-size: 0.83rem; margin-top: 0.22rem; }
.dialog-content { padding: 1rem 1.13rem; }
.dialog-content p { line-height: 1.5; }
.dialog-footer { border-top: 1px solid var(--line); justify-content: end; }
#template-editor .dialog-footer { justify-content: space-between; }
.dialog-validation:not(:empty) { margin: 0 1.13rem 0.8rem; color: #8a3324; font-size: 0.85rem; }
.settings-nav { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.library-list { list-style: none; padding: 0; margin: 0.6rem 0 0; }
.library-row { display: flex; align-items: center; flex-wrap: wrap; justify-content: space-between; gap: 0.45rem; padding: 0.7rem 0; border-bottom: 1px solid var(--line); }
.library-row:last-child { border-bottom: 0; }
.library-row small { color: var(--muted); display: block; margin-top: 0.12rem; }
.template-columns { display: grid; grid-template-columns: 1.12fr 1fr; gap: 0.85rem; }
.template-preview { padding: 0.75rem; border: 1px solid var(--line); border-radius: 9px; background: #f9fbfa; overflow-wrap: anywhere; }
.template-preview h3 { font-size: 0.9rem; }
.template-preview p { font-size: 0.9rem; margin-top: 0.5em; }
.tokens { display: flex; gap: 0.28rem; flex-wrap: wrap; }
.tokens code { color: var(--accent-dark); background: #e9f4ee; padding: 0.15rem 0.24rem; border-radius: 4px; }
.notice { display: none; position: fixed; z-index: 3; bottom: 1rem; left: 1rem; max-width: min(460px, calc(100% - 2rem)); border-radius: 8px; padding: 0.72rem 0.98rem; color: white; background: #173e34; box-shadow: 0 7px 20px #153b2933; font-size: 0.9rem; }
.notice.visible { display: block; }

@media (max-width: 880px) {
  .workspace-grid { grid-template-columns: minmax(0, 1fr); }
  .preview-pane { position: static; max-height: none; }
  .mobile-jump { display: inline-block; font-weight: 650; font-size: 0.88rem; }
  #paper { padding: clamp(0.95rem, 4vw, 2rem); }
}

@media (max-width: 520px) {
  .site-header { padding: 0.63rem 0.7rem; gap: 0.5rem; }
  .brand { gap: 0.45rem; }
  .brand-mark { height: 31px; flex-basis: 31px; font-size: 1.43rem; border-radius: 9px; }
  .brand small { display: none; }
  .brand strong { font-size: 0.87rem; }
  .header-actions { gap: 0.25rem; justify-content: end; }
  .header-actions .button { font-size: 0.75rem; padding: 0.47rem 0.35rem; }
  #workspace { padding: 1.25rem 0.7rem 2rem; }
  .workspace-heading { margin-bottom: 1.05rem; }
  .card { padding: 0.86rem; }
  .template-columns { grid-template-columns: minmax(0, 1fr); }
  .preview-pane { padding: 0.7rem; }
}

@page { size: A4 portrait; margin: 13mm 13mm 14mm; }

@media print {
  :root, body { background: white; color: #101814; }
  body { min-width: 0; }
  body:not(.finalized) #workspace { display: none; }
  body:not(.finalized) #print-blocked { display: block; font: 14pt Arial, sans-serif; }
  .site-header, .workspace-heading, #editor, .preview-toolbar, #review, .mobile-jump, .notice, dialog { display: none !important; }
  #workspace, .workspace-grid, .preview-pane, .paper-frame {
    display: block;
    position: static;
    overflow: visible;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: 0;
    background: white;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }
  #paper { padding: 0; font-size: 12pt; line-height: 1.36; color: #101814; }
  .paper-heading { break-inside: avoid; border-color: #252f2a; }
  .paper-section h3, .paper-section h4, .insulin-name, .dose-summary, .dose-table caption { break-after: avoid; color: #101814; }
  .table-scroll { overflow: visible; }
  .dose-table { table-layout: auto; width: 100%; font-size: 11.5pt; }
  .dose-table[data-meals] { min-width: 0; }
  .dose-table thead { display: table-header-group; }
  .dose-table tbody, .dose-table tr { break-inside: avoid; }
  .dose-table th, .dose-table td { border-color: #56635d; }
  .paper-education { break-before: page; }
  .teaching p, li { orphans: 2; widows: 2; }
}
