:root {
    --font-handwriting: 'Patrick Hand', 'Segoe Print', 'Comic Sans MS', cursive;
    --font-size: 28px;
    --line-unit: 40px;
    --line-top: 0px;
    --line-mid: 10px;
    --line-base: 30px;
    --line-bottom: 40px;
    --line-sky: #7eb6d4;
    --line-mid-color: #a8cce0;
    --line-base-color: #d64545;
    --line-descender: #7eb6d4;
    --notebook-bg: #f7fbff;
    --notebook-margin: #d64545;
    --notebook-margin-pad: 8mm;
    --box-border: #333;
    --page-width: 210mm;
    --practice-rows: 7;
    --row-gap: 18px;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #eef1f5;
    color: #222;
}

.app {
    max-width: 960px;
    margin: 0 auto;
    padding: 20px;
}

/* Toolbar */
.toolbar {
    background: #fff;
    border-radius: 12px;
    padding: 20px 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    margin-bottom: 24px;
}

.toolbar h1 {
    margin: 0 0 16px;
    font-size: 1.35rem;
    color: #1a3a5c;
}

.toolbar-grid {
    display: grid;
    gap: 14px;
}

.field label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 6px;
    color: #444;
}

.field input,
.field select,
.field textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ccd3dc;
    border-radius: 8px;
    font-size: 0.95rem;
    font-family: inherit;
}

.field textarea {
    resize: vertical;
    min-height: 100px;
}

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    padding-top: 4px;
}

.btn {
    padding: 10px 18px;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s;
}

.btn:hover {
    opacity: 0.88;
}

.btn-primary {
    background: #2563eb;
    color: #fff;
}

.btn-secondary {
    background: #059669;
    color: #fff;
}

.btn-danger {
    background: #dc2626;
    color: #fff;
}

.btn-print {
    background: #475569;
    color: #fff;
}

.status-msg {
    margin: 12px 0 0;
    font-size: 0.85rem;
    min-height: 1.2em;
    color: #059669;
}

.status-msg.error {
    color: #dc2626;
}

/* Worksheet */
.worksheet-wrapper {
    display: flex;
    justify-content: center;
}

.worksheet {
    width: var(--page-width);
    max-width: 100%;
    background: #fff;
    padding: 18mm 14mm 12mm;
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.1);
    min-height: 297mm;
}

.worksheet-content {
    margin-bottom: 8mm;
}

/* Güzel yazı defteri çizgileri */
.handwriting-lines {
    position: relative;
    height: var(--line-unit);
    background-color: var(--notebook-bg);
}

.handwriting-lines::before {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    height: 1px;
    pointer-events: none;
    box-shadow:
        0 var(--line-top) 0 0 var(--line-sky),
        0 var(--line-base) 0 1px var(--line-base-color),
        0 var(--line-bottom) 0 0 var(--line-descender);
}

.handwriting-lines::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    top: var(--line-mid);
    height: 0;
    border-top: 1px dashed var(--line-mid-color);
    pointer-events: none;
}

/* Text and box sections */
.text-section,
.boxes-section,
.practice-area {
    padding-left: var(--notebook-margin-pad);
    border-left: 2px solid var(--notebook-margin);
}

.text-section {
    margin-bottom: 10mm;
}

.text-section .text-row {
    margin-bottom: var(--row-gap);
}

.boxes-section {
    margin-bottom: 6mm;
}

.boxes-section .box-row {
    margin-bottom: var(--row-gap);
}

/* Mod 2: satır satır metin + kutucuk */
.paired-section {
    margin-bottom: 6mm;
    padding-left: var(--notebook-margin-pad);
    border-left: 2px solid var(--notebook-margin);
}

.line-block {
    margin-bottom: calc(var(--row-gap) + 4px);
}

.line-block:last-child {
    margin-bottom: 0;
}

.line-block .text-row {
    margin-bottom: 4px;
}

.line-block .box-row {
    margin-bottom: 0;
}

.text-row {
    white-space: nowrap;
    overflow: hidden;
}

.text-row-inner {
    display: block;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    padding-bottom: calc(var(--line-unit) - var(--line-base) - 1px);
    font-family: var(--font-handwriting);
    font-size: var(--font-size);
    line-height: 1;
}

.text-row-inner--aligned {
    display: flex;
    align-items: flex-end;
}

.text-segment {
    flex-shrink: 0;
    overflow: hidden;
    white-space: nowrap;
}

.text-segment-word {
    text-align: left;
}

.text-segment-punct {
    text-align: center;
}

.text-segment-space {
    flex-shrink: 0;
}

/* Box row */
.box-row {
    display: flex;
    align-items: flex-end;
    height: var(--line-unit);
}

.box-cell {
    flex-shrink: 0;
    height: var(--line-unit);
    position: relative;
}

.box-cell.empty {
    height: var(--line-unit);
}

.box-shape {
    position: absolute;
    left: 2px;
    right: 2px;
    border: 1px solid var(--box-border);
    box-sizing: border-box;
    background: #fff;
    border-radius: 1px;
}

.box-shape.ascender {
    top: var(--line-top);
    bottom: calc(var(--line-unit) - var(--line-base));
}

.box-shape.standard {
    top: var(--line-mid);
    bottom: calc(var(--line-unit) - var(--line-base));
}

.box-shape.descender {
    top: var(--line-mid);
    bottom: 0;
}

.box-shape.small-top {
    top: var(--line-top);
    bottom: calc(var(--line-unit) - var(--line-mid));
}

/* Practice notebook area */
.practice-area {
    margin-top: 8mm;
    padding: 6mm 4mm 6mm var(--notebook-margin-pad);
    border: 1px solid #c5d9e8;
    border-left: 2px solid var(--notebook-margin);
    border-radius: 3px;
    background: linear-gradient(180deg, #fafdff 0%, #f3f9ff 100%);
}

.practice-line {
    margin-bottom: var(--row-gap);
}

.practice-line:last-child {
    margin-bottom: 0;
}

/* Footer */
.worksheet-footer {
    margin-top: 12mm;
    text-align: center;
    font-size: 10px;
    font-weight: 600;
    color: #444;
    letter-spacing: 0.3px;
}

#measureCanvas {
    position: absolute;
    visibility: hidden;
    pointer-events: none;
}

/* Print */
@media print {
    body {
        background: #fff;
    }

    .no-print {
        display: none !important;
    }

    .app {
        max-width: none;
        padding: 0;
    }

    .worksheet-wrapper {
        display: block;
    }

    .worksheet,
    .handwriting-lines,
    .practice-area,
    .practice-line,
    .text-section,
    .boxes-section,
    .paired-section,
    .box-shape {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    .handwriting-lines {
        background-color: #f7fbff !important;
    }

    .handwriting-lines::before {
        box-shadow:
            0 var(--line-top) 0 0 #5a9ab8 !important,
            0 var(--line-base) 0 1px #c0392b !important,
            0 var(--line-bottom) 0 0 #5a9ab8 !important;
    }

    .handwriting-lines::after {
        border-top-color: #5a9ab8 !important;
    }

    .text-section,
    .boxes-section,
    .paired-section,
    .practice-area {
        border-left-color: #c0392b !important;
    }

    .practice-area {
        background: #f7fbff !important;
        border-color: #a8cce0 !important;
        border-left-color: #c0392b !important;
    }

    .worksheet {
        width: 100%;
        max-width: none;
        box-shadow: none;
        padding: 10mm 12mm 8mm;
        min-height: auto;
    }

    .worksheet-footer {
        color: #000 !important;
        font-weight: 700;
    }

    @page {
        size: A4 portrait;
        margin: 0;
    }
}

@media (min-width: 640px) {
    .toolbar-grid {
        grid-template-columns: 1fr 1fr;
    }

    .field-wide {
        grid-column: 1 / -1;
    }

    .actions {
        grid-column: 1 / -1;
    }
}
