/* Screen builder UI; .mdt-sheet is the only printed content. */
.migraine-diary-template-page {
    --mdt-ink: #B9503A;
    --tool-peek-bg: #B9503A;
}

.mdt-container { max-width: 820px; margin: auto; padding: 64px 24px; }
.mdt-title { font-size: clamp(2rem, 5vw, 3.25rem); line-height: 1.12; text-wrap: balance; margin: 0 0 20px; }
.mdt-lede { font-size: 1.1rem; line-height: 1.7; max-width: 690px; margin-bottom: 30px; text-wrap: pretty; }

.mdt-form { display: grid; gap: 24px; padding-block: 12px 24px; }
.mdt-field { border: 0; padding: 0; margin: 0; min-width: 0; }
.mdt-field legend { font-weight: 700; margin-bottom: 12px; }
.mdt-options { display: flex; flex-wrap: wrap; gap: 10px; }
.mdt-options input { position: absolute; opacity: 0; }
.mdt-options label {
    display: flex; align-items: center; gap: 8px; min-height: 44px;
    padding: 10px 16px; border-radius: 12px; background: #fff;
    box-shadow: 0 1px 4px rgb(0 0 0 / .08); cursor: pointer;
}
.mdt-options input:checked + label { background: var(--mdt-ink); color: #fff; }
.mdt-options input:focus-visible + label { outline: 2px solid var(--mdt-ink); outline-offset: 3px; }

.mdt-custom { margin-top: 14px; display: grid; gap: 7px; max-width: 340px; }
.mdt-custom[hidden] { display: none; }
.mdt-custom label { font-size: .85rem; font-weight: 600; }
.mdt-custom input {
    box-sizing: border-box; width: 100%; min-width: 0; min-height: 44px;
    padding: 10px 12px; border: 1px solid #ccc; border-radius: 10px;
    background: #fff; color: #222; font: inherit;
}
.mdt-custom input:focus-visible { outline: 2px solid var(--mdt-ink); outline-offset: 2px; }

.mdt-details { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.mdt-detail { display: grid; gap: 7px; min-width: 0; }
.mdt-detail label { font-size: .85rem; }
.mdt-detail input {
    box-sizing: border-box; width: 100%; min-width: 0; min-height: 44px;
    padding: 10px 12px; border: 1px solid #ccc; border-radius: 10px;
    background: #fff; color: #222; font: inherit;
}
.mdt-detail:first-child { grid-column: 1 / -1; }
.mdt-detail input:focus-visible { outline: 2px solid var(--mdt-ink); outline-offset: 2px; }

/* Prevent toolkit's fieldset label counter from repeating inside text-input groups. */
.migraine-diary-template-page .tk-app .tk-q .mdt-detail label::before,
.migraine-diary-template-page .tk-app .tk-q .mdt-custom label::before {
    content: none !important;
    counter-increment: none;
}

.mdt-result-label { text-transform: uppercase; font-size: .75rem; letter-spacing: .1em; }
.mdt-result-headline { font-size: 1.8rem; font-weight: 700; margin: 8px 0 12px; }
.mdt-result-body { line-height: 1.6; }
.mdt-warn {
    margin: 12px 0 0; padding: 10px 14px; border-radius: 10px;
    background: #fff4e8; border: 1px solid #e5b887; color: #6e3d12;
    font-size: .9rem; line-height: 1.5;
}

.mdt-print-actions { display: flex; gap: 18px; align-items: center; margin: 20px 0 28px; }
.mdt-print-actions button {
    flex-shrink: 0; min-height: 48px; padding: 12px 20px; border: 0;
    border-radius: 14px; background: var(--mdt-ink); color: #fff;
    font: inherit; font-weight: 700; cursor: pointer; transition: transform .15s ease;
}
.mdt-print-actions button:active { transform: scale(.96); }
.mdt-print-actions button:focus-visible { outline: 2px solid var(--mdt-ink); outline-offset: 3px; }
.mdt-print-actions p { font-size: .8rem; line-height: 1.5; margin: 0; }

/* ----------------------------------------------------- printable sheet UI */
.mdt-sheet {
    --sheet-content: 840px;
    box-sizing: border-box;
    padding: 26px;
    background: #fff;
    color: #222;
    border: 1px solid #ddd;
    box-shadow: 0 8px 24px rgb(0 0 0 / .06);
    font-variant-numeric: tabular-nums;
}
.mdt-sheet[data-paper="letter"] { --sheet-content: 770px; }
.mdt-sheet .mdt-sheet-kicker { font-size: .65rem; letter-spacing: .12em; margin: 0; color: #555; }
.mdt-sheet h2 {
    font: 700 clamp(1.15rem, 3vw, 1.85rem)/1.25 var(--font-ui);
    overflow-wrap: anywhere; margin: 5px 0 14px;
}
.mdt-sheet-meta { display: grid; grid-template-columns: 1fr 1fr 1.3fr; gap: 8px 16px; margin-bottom: 18px; }
.mdt-sheet .mdt-write { display: flex; align-items: baseline; gap: 8px; margin: 0; font-size: .72rem; min-width: 0; }
.mdt-write span { flex-shrink: 0; }
.mdt-write b {
    font-weight: 400; min-width: 0; flex: 1; min-height: 1.45em;
    border-bottom: 1px solid #999; overflow-wrap: anywhere;
}
.mdt-sheet-content { min-height: var(--sheet-content); display: flex; flex-direction: column; gap: 14px; }

/* Layout 1: 31-day calendar grid */
.mdt-cal-grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 5px;
    flex: 1;
}
.mdt-cal-day {
    border: 1px solid #999;
    padding: 5px 6px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 5px;
    min-height: 76px;
    font-size: .64rem;
    line-height: 1.3;
}
.mdt-cal-day.is-opt { border-style: dashed; }
.mdt-cal-top { display: flex; justify-content: space-between; align-items: baseline; gap: 4px; }
.mdt-cal-top strong { font-size: .78rem; }
.mdt-cal-mid { display: flex; flex-direction: column; gap: 3px; }
.mdt-cal-bot { border-top: 1px dotted #ccc; padding-top: 3px; color: #333; }
.mdt-cal-key {
    grid-column: span 4;
    border: 1px solid #999;
    background: #fafafa;
    padding: 7px 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    font-size: .64rem;
    line-height: 1.35;
}
.mdt-cal-key p { margin: 0; }
.mdt-key-title { font-weight: 700; letter-spacing: .06em; font-size: .6rem; }
.mdt-key-note { color: #444; }

/* Totals block */
body.page-sketch .mdt-totals, .mdt-totals {
    margin: 0; padding: 10px 12px; background: #fff; border: 1.5px solid #333;
}
.mdt-totals-head { margin: 0 0 8px; font-size: .65rem; font-weight: 700; letter-spacing: .08em; }
.mdt-totals-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.mdt-total-box { border-left: 2px solid #999; padding-left: 8px; display: flex; flex-direction: column; gap: 3px; }
.mdt-total-box:first-child { border-left: 0; padding-left: 0; }
.mdt-total-box span { font-size: .66rem; font-weight: 700; line-height: 1.3; }
.mdt-total-box b { font-size: 1rem; font-weight: 400; letter-spacing: .04em; }
.mdt-total-box small { font-size: .58rem; color: #444; line-height: 1.3; }

/* Layout 2: Per-attack cards */
.mdt-attacks-list { display: flex; flex-direction: column; gap: 10px; flex: 1; }
body.page-sketch .mdt-attack, .mdt-attack {
    flex: 1; display: flex; flex-direction: column; justify-content: space-between;
    margin: 0; padding: 9px 11px; background: #fff; border: 1px solid #999;
    font-size: .68rem; line-height: 1.45; gap: 5px;
}
.mdt-attack-row { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: baseline; }
.mdt-attack-top { padding-bottom: 4px; border-bottom: 1px solid #ccc; font-weight: 600; }
.mdt-attack-num { font-weight: 700; font-size: .74rem; }
.mdt-attack-note { display: flex; flex-wrap: nowrap; align-items: baseline; gap: 8px; }
.mdt-attack-note span { flex-shrink: 0; }
.mdt-attack-note b { flex: 1; min-width: 0; min-height: 1.3em; border-bottom: 1px solid #999; }

body.page-sketch .mdt-clear-strip, .mdt-clear-strip {
    margin: 0; padding: 8px 10px; background: #fff; border: 1px solid #999; font-size: .66rem;
}
.mdt-clear-strip p { margin: 0 0 6px; }
.mdt-clear-nums { display: grid; grid-template-columns: repeat(31, minmax(0, 1fr)); gap: 2px; text-align: center; }
.mdt-clear-nums span { border: 1px solid #ccc; padding: 3px 0; font-size: .62rem; }

/* Layout 3: Single-trigger 2-column 31-day log */
.mdt-sheet-rule { margin: 0; font-size: .68rem; line-height: 1.45; color: #333; }
.mdt-trig-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; flex: 1; align-items: start; }
.mdt-trig-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: .62rem; }
.mdt-trig-table th, .mdt-trig-table td { border: 1px solid #999; padding: 4px 5px; text-align: left; }
.mdt-trig-table th { font-size: .6rem; background: #f5f5f5; overflow-wrap: anywhere; }
.mdt-trig-table td { height: 22px; }
.mdt-col-day { width: 10%; text-align: center !important; font-weight: 700; }
.mdt-col-mark { width: 31%; }
.mdt-col-pain { width: 11%; }
.mdt-col-med { width: 25%; }
.mdt-col-test { width: 23%; }
.mdt-row-note td { background: #fafafa; font-size: .62rem; }

.mdt-sheet-footer {
    display: flex; justify-content: space-between; gap: 10px;
    border-top: 1px solid #999; padding-top: 10px; margin-top: 16px; font-size: .64rem;
}

.mdt-prose { margin-top: 44px; line-height: 1.8; }
.mdt-prose h2 { margin-top: 30px; text-wrap: balance; }

@media (max-width: 720px) {
    .mdt-cal-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .mdt-cal-key { grid-column: 1 / -1; }
    .mdt-totals-grid { grid-template-columns: 1fr; gap: 8px; }
    .mdt-total-box { border-left: 0; border-top: 1px solid #ddd; padding-left: 0; padding-top: 6px; }
    .mdt-total-box:first-child { border-top: 0; padding-top: 0; }
    .mdt-trig-cols { grid-template-columns: 1fr; }
    .mdt-clear-nums { grid-template-columns: repeat(11, minmax(0, 1fr)); }
}

@media (max-width: 600px) {
    .mdt-container { padding: 40px 16px; }
    .mdt-options { flex-direction: column; }
    .mdt-options label { width: 100%; box-sizing: border-box; }
    .mdt-details { grid-template-columns: 1fr; }
    .mdt-custom { max-width: none; }
    .mdt-print-actions { flex-direction: column; align-items: stretch; }
    .mdt-sheet { padding: 12px; --sheet-content: 520px; }
    .mdt-sheet[data-paper="letter"] { --sheet-content: 480px; }
    .mdt-sheet-meta { grid-template-columns: 1fr; }
    .mdt-sheet-footer { font-size: .56rem; flex-direction: column; gap: 4px; }
}

@media print {
    html, body { margin: 0 !important; padding: 0 !important; background: #fff !important; }
    body.migraine-diary-template-page > :not(main),
    main > :not(.mdt-container),
    .mdt-container > :not(.migraine-diary-template),
    .migraine-diary-template .tk-app > :not(.tk-r),
    .migraine-diary-template .tk-r > :not(.mdt-result),
    .mdt-result > :not(.mdt-sheet) { display: none !important; }
    main, .mdt-container, .migraine-diary-template, .migraine-diary-template .tk-app,
    .migraine-diary-template .tk-r, .mdt-result {
        display: block !important; position: static !important;
        margin: 0 !important; padding: 0 !important;
        width: 100% !important; max-width: none !important;
        height: auto !important; max-height: none !important;
        overflow: visible !important; border: 0 !important; box-shadow: none !important;
        background: #fff !important; transform: none !important; opacity: 1 !important;
    }
    .mdt-result *, .mdt-result *::before, .mdt-result *::after {
        animation: none !important; transition: none !important;
        opacity: 1 !important; transform: none !important;
    }
    /* Leave room for hardware margins and browser print headers/footers. */
    .mdt-sheet {
        display: flex; flex-direction: column;
        height: 252mm; padding: 0; margin: 0; border: 0; box-shadow: none;
        font-family: Arial, sans-serif; break-inside: avoid;
    }
    .mdt-sheet[data-paper="letter"] { height: 234mm; }
    .mdt-sheet-head { flex-shrink: 0; }
    .mdt-sheet h2 { font: 700 18pt/1.2 Arial, sans-serif; margin: 2mm 0 3mm; }
    .mdt-sheet .mdt-sheet-kicker { font-size: 7.5pt; }
    .mdt-sheet-meta { grid-template-columns: 1fr 1fr 1.3fr; margin-bottom: 4mm; gap: 3mm 5mm; }
    .mdt-sheet .mdt-write { font-size: 8.5pt; }
    .mdt-sheet-content { min-height: 0; flex: 1; gap: 3.5mm; }
    .mdt-cal-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 1.5mm; }
    .mdt-cal-day { min-height: 0; padding: 1.8mm; font-size: 7.2pt; gap: 1mm; }
    .mdt-cal-top strong { font-size: 8.5pt; }
    .mdt-cal-key { grid-column: span 4; padding: 2mm 3mm; font-size: 7.2pt; }
    .mdt-totals { padding: 2.5mm 3mm; }
    .mdt-totals-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .mdt-total-box span { font-size: 7.5pt; }
    .mdt-total-box b { font-size: 11pt; }
    .mdt-total-box small { font-size: 6.5pt; }
    .mdt-attacks-list { gap: 2.5mm; }
    .mdt-attack { padding: 2.5mm 3mm; font-size: 7.8pt; gap: 1.2mm; }
    .mdt-clear-strip { padding: 2mm 3mm; font-size: 7.5pt; }
    .mdt-clear-nums { grid-template-columns: repeat(31, minmax(0, 1fr)); }
    .mdt-clear-nums span { padding: 1mm 0; font-size: 7pt; }
    .mdt-trig-cols { grid-template-columns: 1fr 1fr; gap: 3.5mm; }
    .mdt-trig-table { font-size: 7.4pt; }
    .mdt-trig-table td { height: 9.2mm; padding: 0.8mm 1.5mm; }
    .mdt-sheet[data-paper="letter"] .mdt-trig-table td { height: 8.1mm; }
    .mdt-sheet-rule { font-size: 8pt; }
    .mdt-sheet-footer { flex-direction: row; font-size: 7.5pt; margin-top: 3mm; padding-top: 2mm; }
}

@media (prefers-reduced-motion: reduce) {
    .mdt-print-actions button { transition: none; }
}
