/* Koutab app UI (the printable pages live in sheet.css). */
:root {
    --ui-bg: #eef1ec;
    --ui-panel: #ffffff;
    --ui-ink: #1f2a22;
    --ui-muted: #66736a;
    --ui-line: #dde3dc;
    --ui-brand: #2e7d32;
    --ui-brand-2: #f9a825;
    --ui-brand-soft: #e6f2e5;
    --ui-radius: 12px;
    --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--ui-font); color: var(--ui-ink); background: var(--ui-bg); display: flex; flex-direction: column; font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.ar, [dir='rtl'] { font-family: 'Amiri', serif; }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 16px; background: var(--ui-brand); color: #fff; flex: none; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; }
.brand-ar { font-family: 'Amiri', serif; font-size: 30px; line-height: 1; font-weight: 700; }
.brand-en { display: flex; flex-direction: column; font-weight: 700; font-size: 17px; line-height: 1.1; }
.brand-en small { font-weight: 400; font-size: 11px; opacity: 0.85; }
.tb-actions { display: flex; gap: 8px; }
.tb-actions button { border: 1px solid rgba(255, 255, 255, 0.35); background: rgba(255, 255, 255, 0.12); color: #fff; padding: 8px 14px; border-radius: 999px; font-weight: 600; }
.tb-actions button:hover { background: rgba(255, 255, 255, 0.22); }
.tb-actions .coffee { display: inline-flex; align-items: center; gap: 6px; color: #fff; text-decoration: none; font-weight: 600; padding: 8px 12px; border-radius: 999px; opacity: 0.9; }
.tb-actions .coffee:hover { opacity: 1; background: rgba(255, 255, 255, 0.12); }
.tb-actions button.primary { background: var(--ui-brand-2); border-color: var(--ui-brand-2); color: #2b1d00; }
.tb-actions button.primary:hover { filter: brightness(1.05); }

/* ---------- layout ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }
.panel { width: 370px; flex: none; background: var(--ui-panel); border-right: 1px solid var(--ui-line); display: flex; flex-direction: column; min-height: 0; }
.preview { flex: 1; overflow: auto; min-width: 0; display: flex; flex-direction: column; }

.gen-current { display: flex; align-items: center; gap: 10px; margin: 12px; padding: 10px 12px; border: 2px solid var(--ui-brand); border-radius: var(--ui-radius); background: var(--ui-brand-soft); text-align: left; }
.gen-current:hover { background: #dcecda; }
.gen-icon { font-size: 28px; line-height: 1; }
.gen-names { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.gen-names b { font-size: 15px; }
.gen-names span { font-size: 17px; color: var(--ui-brand); text-align: left; }
.gen-change { font-size: 12px; font-weight: 700; color: var(--ui-brand); white-space: nowrap; }

.tabs { display: flex; gap: 4px; padding: 0 12px; border-bottom: 1px solid var(--ui-line); }
.tabs button { flex: 1; border: 0; background: none; padding: 10px 4px; font-weight: 600; color: var(--ui-muted); border-bottom: 3px solid transparent; }
.tabs button.on { color: var(--ui-brand); border-bottom-color: var(--ui-brand); }
.tab-body { flex: 1; overflow: auto; padding: 12px 16px 24px; }
.panel-foot { display: flex; justify-content: space-between; padding: 8px 16px; border-top: 1px solid var(--ui-line); }
.blurb { margin: 0 0 12px; color: var(--ui-muted); }

/* ---------- fields ---------- */
.group { margin-bottom: 18px; }
.group h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ui-muted); margin: 6px 0 10px; }
.field { margin-bottom: 14px; }
.field[hidden] { display: none; }
.field-label { display: block; font-weight: 600; margin-bottom: 6px; }
.help { margin: 5px 0 0; font-size: 12px; color: var(--ui-muted); }
input[type='text'], input[type='number'], select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--ui-line); border-radius: 8px; background: #fff; }
textarea { resize: vertical; }
textarea.ar, input.ar { font-size: 20px; line-height: 1.6; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--ui-brand); outline-offset: 1px; }
.range { display: flex; align-items: center; gap: 10px; }
.range input { flex: 1; accent-color: var(--ui-brand); }
.range output { min-width: 52px; text-align: right; font-variant-numeric: tabular-nums; color: var(--ui-muted); }
.seg { display: flex; flex-wrap: wrap; gap: 4px; background: #f1f4f0; padding: 3px; border-radius: 10px; }
.seg button { flex: 1 1 auto; border: 0; background: none; padding: 6px 8px; border-radius: 8px; font-size: 13px; color: var(--ui-muted); white-space: nowrap; }
.seg button.on { background: #fff; color: var(--ui-ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-weight: 500; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track { width: 36px; height: 20px; border-radius: 99px; background: #cfd6cf; position: relative; flex: none; transition: background 0.15s; }
.switch .track::after { content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform 0.15s; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); }
.switch input:checked + .track { background: var(--ui-brand); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--ui-brand); outline-offset: 2px; }

.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip { width: 38px; height: 38px; border: 1px solid var(--ui-line); background: #fff; border-radius: 9px; font-family: 'Amiri', serif; font-size: 22px; line-height: 1; padding: 0; }
.chip.wide { width: auto; padding: 0 10px; font-family: var(--ui-font); font-size: 13px; height: 32px; }
.chip.on { background: var(--ui-brand); border-color: var(--ui-brand); color: #fff; }
.chip-actions { display: flex; gap: 6px; margin-top: 8px; }
.chip-actions button, .word-tools button, .ghost, .dlg-foot button, .upload { border: 1px solid var(--ui-line); background: #fff; border-radius: 8px; padding: 5px 10px; font-size: 13px; }
.chip-actions button:hover, .word-tools button:hover, .ghost:hover { border-color: var(--ui-brand); color: var(--ui-brand); }
.ghost.full { width: 100%; padding: 10px; font-weight: 600; margin-top: 4px; }
.word-tools { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.link { border: 0; background: none; color: var(--ui-brand); padding: 0; font-size: 13px; text-decoration: underline; }
button.icon { border: 0; background: none; font-size: 18px; padding: 4px 8px; border-radius: 8px; }
button.icon:hover { background: #f1f4f0; }
button.primary { background: var(--ui-brand); color: #fff; border: 0; border-radius: 8px; padding: 8px 14px; font-weight: 600; }

/* keyboard */
.kb-wrap { position: relative; }
.kb-toggle { position: absolute; top: 6px; left: 6px; border: 1px solid var(--ui-line); background: #fff; border-radius: 6px; font-size: 11px; padding: 2px 6px; color: var(--ui-muted); }
.kb-toggle.on { background: var(--ui-brand); color: #fff; border-color: var(--ui-brand); }
.kb-wrap input.ar { padding-left: 64px; }
.kb { margin-top: 6px; padding: 6px; background: #f1f4f0; border-radius: 10px; display: flex; flex-direction: column; gap: 4px; }
.kb-row { display: flex; gap: 3px; justify-content: center; flex-wrap: wrap; }
.kb button { min-width: 25px; height: 32px; border: 1px solid var(--ui-line); background: #fff; border-radius: 6px; font-family: 'Amiri', serif; font-size: 18px; padding: 0 3px; line-height: 1; }
.kb button:active { background: var(--ui-brand-soft); }
.kb button.mark { color: var(--ui-brand); }
.kb button.wide { min-width: 70px; font-family: var(--ui-font); font-size: 12px; }

/* style tab */
.themes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.theme { border: 2px solid var(--ui-line); background: #fff; border-radius: 10px; padding: 6px 4px 4px; display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; }
.theme span { width: 14px; height: 14px; border-radius: 50%; }
.theme em { width: 100%; font-style: normal; font-size: 11px; color: var(--ui-muted); margin-top: 2px; }
.theme.on { border-color: var(--ui-brand); }
.colors { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.color { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--ui-muted); text-align: center; }
.color input { width: 40px; height: 32px; border: 1px solid var(--ui-line); border-radius: 8px; padding: 2px; background: #fff; cursor: pointer; }
.fonts { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.font { border: 2px solid var(--ui-line); background: #fff; border-radius: 10px; padding: 6px; display: flex; flex-direction: column; align-items: center; }
.font b { font-size: 22px; font-weight: 400; line-height: 1.5; }
.font span { font-size: 11px; color: var(--ui-muted); text-align: center; }
.font.on { border-color: var(--ui-brand); background: var(--ui-brand-soft); }
.pic-btn { display: flex; align-items: center; gap: 10px; border: 1px solid var(--ui-line); background: #fff; border-radius: 8px; padding: 4px 10px 4px 4px; }
.pic-btn .pic { width: 34px !important; height: 34px !important; }
.pic-btn .none { width: 34px; text-align: center; color: var(--ui-muted); }

/* ---------- preview ---------- */
.preview-bar { position: sticky; top: 0; z-index: 2; display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; background: rgba(238, 241, 236, 0.92); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); color: var(--ui-muted); font-weight: 600; }
.zoom { display: flex; align-items: center; gap: 4px; }
.zoom button { border: 1px solid var(--ui-line); background: #fff; border-radius: 8px; min-width: 32px; height: 30px; }
#zoomLabel { min-width: 48px; text-align: center; font-variant-numeric: tabular-nums; }
/* Pages keep their real mm size and are scaled with a transform (works the same in every
   browser, unlike CSS zoom); #pagesBox reserves the scaled footprint. */
#pagesBox { position: relative; margin: 8px auto 48px; flex: none; }
#pages { position: absolute; top: 0; left: 0; display: flex; flex-direction: column; gap: 24px; transform-origin: 0 0; }
#pages .page { box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08), 0 10px 30px rgba(0, 0, 0, 0.1); }

/* ---------- dialogs ---------- */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 48px); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
dialog.wide { width: min(1000px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(20, 30, 22, 0.45); }
dialog[open] { display: flex; flex-direction: column; }
.dlg-head { display: flex; align-items: flex-start; gap: 12px; padding: 18px 20px 10px; border-bottom: 1px solid var(--ui-line); }
.dlg-head > :first-child { flex: 1; }
.dlg-head h2 { margin: 0; font-size: 20px; }
.dlg-head p { margin: 4px 0 0; color: var(--ui-muted); }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 20px; border-top: 1px solid var(--ui-line); }
#galleryBody { overflow: auto; padding: 4px 20px 20px; }
#galleryBody h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ui-muted); margin: 16px 0 8px; }
.gal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.gal-card { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; text-align: left; padding: 12px; border: 2px solid var(--ui-line); background: #fff; border-radius: var(--ui-radius); transition: transform 0.1s, border-color 0.1s; }
.gal-card:hover { border-color: var(--ui-brand); transform: translateY(-2px); }
.gal-card.on { border-color: var(--ui-brand); background: var(--ui-brand-soft); }
.gal-icon { font-size: 28px; }
.gal-ar { font-size: 19px; color: var(--ui-brand); align-self: stretch; text-align: right; }
.gal-blurb { font-size: 12px; color: var(--ui-muted); }
.pic-grid { overflow: auto; padding: 16px 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.pic-item { position: relative; border: 1px solid var(--ui-line); background: #fff; border-radius: 10px; padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.pic-item:hover { border-color: var(--ui-brand); }
.pic-item small { font-family: 'Amiri', serif; font-size: 14px; }
.pic-item.mine { background: var(--ui-brand-soft); }
.pic-item .none { font-size: 34px; height: 16mm; display: flex; align-items: center; color: var(--ui-muted); }
.pic-item .del { position: absolute; top: 2px; right: 4px; font-size: 16px; color: #b00; }
.upload { cursor: pointer; align-self: center; }
.word-list { overflow: auto; padding: 12px 20px; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 4px 12px; }
.word-item { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.word-item .ar { font-size: 20px; }
.word-item small { color: var(--ui-muted); }
.preset-save { display: flex; gap: 8px; padding: 14px 20px; }
.preset-list { list-style: none; margin: 0; padding: 0 20px 12px; overflow: auto; }
.preset-list li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--ui-line); }
.preset-list li .link { font-size: 15px; text-decoration: none; font-weight: 600; text-align: left; }
.preset-list li small { flex: 1; color: var(--ui-muted); }
.preset-list .muted { color: var(--ui-muted); }
.about { padding: 6px 20px 16px; line-height: 1.6; overflow: auto; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #1f2a22; color: #fff; padding: 10px 18px; border-radius: 999px; opacity: 0; pointer-events: none; transition: all 0.2s; z-index: 50; max-width: calc(100vw - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- small screens ---------- */
@media (max-width: 860px) {
    body { height: auto; }
    .workspace { flex-direction: column; }
    .panel { width: auto; border-right: 0; border-bottom: 1px solid var(--ui-line); }
    .tab-body { max-height: none; }
    .brand-en small, .tb-actions button span, .tb-actions .coffee span { display: none; }
    .tb-actions button { padding: 8px 12px; }
}

/* ---------- printing: only the pages ---------- */
@media print {
    html, body { background: #fff !important; height: auto; display: block; }
    .topbar, .panel, .preview-bar, dialog, .toast, noscript { display: none !important; }
    .workspace, .preview, #pagesBox, #pages { display: block !important; overflow: visible !important; padding: 0 !important; margin: 0 !important; }
    #pagesBox { width: auto !important; height: auto !important; }
    #pages { position: static !important; transform: none !important; }
    #pages .page { box-shadow: none !important; margin: 0 !important; break-after: page; page-break-after: always; }
    #pages .page:last-child { break-after: auto; page-break-after: auto; }
}
