:root {
    --bg: #f2f2f7;
    --panel: #ffffff;
    --panel-2: #f2f2f7;
    --border: #e3e3e8;
    --text: #111114;
    --muted: #6c6c72;
    --accent: #3f5bd9;
    --accent-text: #ffffff;
    --danger: #c0392b;
    --ok: #1e8e3e;
    --radius: 10px;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
    font-size: 15px;
    color: var(--text);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #000000;
        --panel: #1c1c1e;
        --panel-2: #2c2c2e;
        --border: #38383a;
        --text: #f5f5f7;
        --muted: #a1a1a6;
        --accent: #7d93f2;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body { background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

button {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 12px;
    cursor: pointer;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
input[type="text"], input[type="password"], input[type="number"], select, textarea {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 5px 8px;
}
input[type="number"] { width: 90px; }

.bar {
    position: sticky; top: 0; z-index: 5;
    display: flex; align-items: center; gap: 16px;
    padding: 10px 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
}
.bar .title { font-weight: 700; font-size: 16px; color: var(--text); text-decoration: none; display: inline-flex; align-items: center; gap: 8px; }
.bar .title .logo { width: 28px; height: 28px; }
.bar .back { text-decoration: none; }
.bar .status { margin-left: auto; color: var(--muted); }
.bar .status.error { color: var(--danger); }

main { max-width: 1100px; margin: 0 auto; padding: 24px 16px 64px; }

/* Home: all tools */
.group h2 { font-size: 15px; margin: 24px 0 10px; color: var(--muted); font-weight: 600; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.card {
    display: block; text-align: left;
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 14px; color: var(--text); text-decoration: none;
}
.card:hover { border-color: var(--accent); }
.card strong { display: block; margin-bottom: 4px; }
.card span { color: var(--muted); font-size: 13px; }

/* One tool */
.tool { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.tool h1 { margin: 0 0 4px; font-size: 20px; }
.tool > p.lead { margin: 0 0 16px; color: var(--muted); }
.drop {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
    border: 2px dashed var(--border); border-radius: var(--radius);
    padding: 28px; text-align: center; color: var(--muted);
}
.drop.over { border-color: var(--accent); color: var(--text); }
.files { list-style: none; padding: 0; margin: 12px 0; display: grid; gap: 6px; }
.files li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; background: var(--panel-2); border-radius: 6px; }
.files li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files li .size { color: var(--muted); font-size: 12px; }
.files li button { padding: 2px 8px; }
.options { display: grid; gap: 10px; margin: 16px 0; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row > label:first-child, .row > .label { min-width: 150px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 6px; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0; }
.actions { display: flex; gap: 10px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.result { margin-top: 16px; padding: 12px; border-radius: 8px; background: var(--panel-2); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.result .ok { color: var(--ok); font-weight: 600; }

/* Page thumbnails */
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; margin: 12px 0; }
.thumb {
    position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 8px; border: 2px solid transparent; border-radius: 8px; background: var(--panel-2); cursor: pointer;
}
.thumb.selected { border-color: var(--accent); }
.thumb.dragging { opacity: .4; }
.thumb.drop-before { box-shadow: -4px 0 0 var(--accent); }
.thumb .pic { width: 100px; height: 130px; display: flex; align-items: center; justify-content: center; }
.thumb .pic canvas, .thumb .pic .blank { max-width: 100px; max-height: 130px; box-shadow: 0 1px 3px rgba(0,0,0,.25); background: #fff; transition: transform .15s; }
.thumb .pic .blank { width: 92px; height: 130px; }
.thumb .label { font-size: 12px; color: var(--muted); }
.thumb .tools { display: flex; gap: 4px; }
.thumb .tools button { padding: 1px 6px; font-size: 12px; }

/* Crop and signature: a page with boxes on it */
.stage { position: relative; display: inline-block; margin: 8px 0; user-select: none; touch-action: none; }
.stage canvas { display: block; max-width: 100%; box-shadow: 0 1px 4px rgba(0,0,0,.3); }
.box { position: absolute; border: 2px solid var(--accent); background: rgba(123, 63, 228, .08); cursor: move; }
.box.crop { box-shadow: 0 0 0 9999px rgba(0,0,0,.35); background: transparent; }
.stage.cropping { overflow: hidden; }
.box .handle { position: absolute; width: 14px; height: 14px; background: var(--accent); border-radius: 3px; }
.box .handle.nw { left: -8px; top: -8px; cursor: nwse-resize; }
.box .handle.ne { right: -8px; top: -8px; cursor: nesw-resize; }
.box .handle.sw { left: -8px; bottom: -8px; cursor: nesw-resize; }
.box .handle.se { right: -8px; bottom: -8px; cursor: nwse-resize; }
.box img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.box .remove { position: absolute; right: -10px; top: -30px; padding: 0 6px; }
.pager { display: flex; gap: 8px; align-items: center; }

.pad { border: 1px solid var(--border); border-radius: 8px; background: #fff; touch-action: none; cursor: crosshair; max-width: 100%; }
.tabs { display: flex; gap: 6px; margin-bottom: 8px; }
.sig-preview { max-height: 80px; background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 4px; }
.fields { display: grid; gap: 8px; }
.fields .row > label:first-child { min-width: 220px; word-break: break-all; }
.preview canvas { max-width: 100%; max-height: 520px; box-shadow: 0 1px 4px rgba(0,0,0,.3); }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); color: var(--text); min-width: 320px; }
dialog::backdrop { background: rgba(0,0,0,.35); }

@media (max-width: 600px) {
    .row > label:first-child, .row > .label { min-width: 100%; }
}

/* Scan */
.scan-strip { display: flex; gap: 10px; overflow-x: auto; padding: 8px 2px 12px; }
.scan-thumb { flex: none; display: grid; gap: 4px; justify-items: center; padding: 6px; border: 2px solid transparent; border-radius: 10px; background: var(--panel-2); }
.scan-thumb.selected { border-color: var(--accent); }
.scan-thumb .pick { padding: 0; border: 0; background: none; }
.scan-thumb canvas { max-width: 90px; max-height: 120px; display: block; box-shadow: 0 1px 3px rgba(0,0,0,.25); background: #fff; }
.scan-thumb .tools { display: flex; gap: 4px; align-items: center; font-size: 12px; color: var(--muted); }
.scan-thumb .tools button { padding: 1px 7px; font-size: 12px; }
.scan-add { flex: none; width: 90px; min-height: 120px; font-size: 28px; border-style: dashed; }
.scan-stage { position: relative; margin: 8px 0; touch-action: none; user-select: none; -webkit-user-select: none; }
.scan-stage > canvas { display: block; border-radius: 6px; }
.scan-outline { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
.scan-outline .shade { fill: rgba(0, 0, 0, .45); }
.scan-outline polygon { fill: none; stroke: #9b6bff; stroke-width: 2.5; }
.scan-handle {
    position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%;
    background: rgba(155, 107, 255, .35); border: 3px solid #fff; box-shadow: 0 0 0 2px #7b3fe4, 0 2px 8px rgba(0,0,0,.4);
    touch-action: none; cursor: grab;
}
.scan-loupe { position: fixed; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px #7b3fe4, 0 6px 20px rgba(0,0,0,.5); pointer-events: none; z-index: 1000; background: #000; }
.scan-preview { margin-top: 10px; display: flex; justify-content: center; }
.scan-preview canvas { box-shadow: 0 1px 6px rgba(0,0,0,.3); background: #fff; }
.scan-look input[type="range"] { flex: 1; max-width: 320px; }
.scan-size { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
@media (max-width: 600px) {
    .tool[data-tool="scan"] .drop { padding: 12px; }
    .tool[data-tool="scan"] .drop span { display: none; }
    .tool[data-tool="scan"] > p.lead { font-size: 13px; }
}


/* ============================================================ iOS look (new UI, pass 3) */

body { -webkit-tap-highlight-color: transparent; letter-spacing: -0.01em; }
.bar { -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px); background: color-mix(in srgb, var(--panel) 92%, transparent); }
button { border-radius: 10px; padding: 8px 14px; }
button.primary { border-radius: 14px; }
input[type="text"], input[type="password"], input[type="number"], select, textarea { border-radius: 10px; padding: 8px 10px; }

/* In the app: no top bar (the tab bar and the back links do its job); messages as a toast */
body.in-app { padding-bottom: calc(84px + env(safe-area-inset-bottom)); padding-top: env(safe-area-inset-top); }
body.in-app .bar { position: static; padding: 0; border: 0; background: transparent; min-height: 0; }
body.in-app .bar .back, body.in-app .bar .title, body.in-app #api-key { display: none; }
body.in-app .bar .status:not(:empty) {
    position: fixed; left: 12px; right: 12px; bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 50; margin: 0;
    padding: 10px 14px; border-radius: 14px; text-align: center; background: #1c1c1e; color: #ffffff; box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}
body.in-app .bar .status.error:not(:empty) { background: #b3261e; color: #ffffff; }

/* Tab bar (in the app) */
.tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
    padding: 7px max(8px, calc(50% - 320px)) calc(8px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--panel) 92%, transparent); border-top: 1px solid var(--border);
    -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--muted); font-size: 10.5px; font-weight: 500; text-decoration: none; }
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a[aria-current="page"] svg { fill: currentColor; fill-opacity: .18; }

main { max-width: 720px; }

/* ------------------------------------------------------------ the tools list */
.tools-home { display: flex; flex-direction: column; gap: 18px; }
.large-title { margin: 6px 4px 0; font-size: 34px; font-weight: 700; letter-spacing: -0.03em; }
.search { display: flex; align-items: center; gap: 8px; background: color-mix(in srgb, var(--muted) 16%, transparent); border-radius: 12px; padding: 9px 12px; color: var(--muted); }
.search svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; flex: none; }
.search input { flex: 1; border: 0; background: transparent; outline: none; font-size: 16px; padding: 0; color: var(--text); }
.tools-home .group h2 { margin: 6px 16px 7px; font-size: 13px; font-weight: 400; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.tools-home .cards { display: flex; flex-direction: column; gap: 0; background: var(--panel); border-radius: 18px; overflow: hidden; }
.tools-home .card {
    display: flex; align-items: center; gap: 14px; border: 0; border-radius: 0; padding: 0 0 0 16px; background: transparent; min-height: 60px;
}
.tools-home .card:hover { background: var(--panel-2); }
.tools-home .card + .card .card-text { border-top: 1px solid var(--border); }
.tools-home .card[hidden] { display: none; }
.tool-icon { flex: none; width: 30px; height: 30px; border-radius: 8px; display: flex; align-items: center; justify-content: center; }
.tool-icon svg { width: 17px; height: 17px; fill: none; stroke: #ffffff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.card-text { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 2px; padding: 10px 0; align-self: stretch; }
.tools-home .card strong { margin: 0; font-size: 16px; font-weight: 500; }
.tools-home .card .card-text span { font-size: 13px; color: var(--muted); line-height: 1.3; }
.chev { flex: none; width: 18px; height: 18px; margin-right: 12px; fill: none; stroke: #b5b5bb; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ------------------------------------------------------------ a tool */
.tool { background: transparent; border: 0; padding: 0; display: flex; flex-direction: column; gap: 14px; }
.tool-nav { margin: 0 0 -6px; }
.tool-back { display: inline-flex; align-items: center; gap: 2px; font-size: 17px; text-decoration: none; padding: 6px 6px 6px 0; }
.tool-back svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.tool h1 { font-size: 30px; font-weight: 700; letter-spacing: -0.03em; margin: 0; }
.tool > p.lead { margin: -8px 0 0; font-size: 15px; }
.tool .drop { background: var(--panel); border: 1.5px dashed color-mix(in srgb, var(--muted) 45%, transparent); border-radius: 18px; padding: 26px 16px; }
.tool .drop button { background: var(--accent); color: #ffffff; border: 0; border-radius: 999px; padding: 10px 20px; font-weight: 600; font-size: 16px; }
.tool .files li { background: var(--panel); border-radius: 12px; padding: 10px 12px; }
.tool .options { background: var(--panel); border-radius: 18px; padding: 14px 16px; margin: 0; gap: 12px; }
.tool .options:empty { display: none; }
.tool .area:empty { display: none; }
.tool .actions { margin-top: 0; }
.tool .actions button.primary { width: 100%; min-height: 52px; font-size: 17px; }
.tool .result { background: var(--panel); border-radius: 18px; padding: 14px 16px; }
.tool .result button.download { width: 100%; min-height: 50px; font-size: 16px; }
.thumbs .thumb { background: var(--panel); border-radius: 14px; }

/* ------------------------------------------------------------ scanner */
.scan-strip { gap: 12px; padding: 4px 2px 10px; }
.scan-thumb { background: var(--panel); border-radius: 14px; border-width: 2.5px; padding: 8px; }
.scan-thumb.selected { border-color: var(--accent); }
.scan-add { border-radius: 14px; border: 1.5px dashed color-mix(in srgb, var(--muted) 45%, transparent); background: transparent; color: var(--accent); }
.scan-stage { background: #000000; border-radius: 18px; padding: 12px; display: flex; justify-content: center; }
.scan-stage > canvas { border-radius: 4px; }
.scan-preview { background: var(--panel); border-radius: 18px; padding: 16px; }
.scan-preview canvas { box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 12px 32px rgba(17,17,20,.12); }
.tool[data-tool="scan"] .tabs { display: grid; grid-template-columns: 1fr 1fr; background: color-mix(in srgb, var(--muted) 16%, transparent); border-radius: 10px; padding: 2px; gap: 2px; }
.tool[data-tool="scan"] .tabs button { border: 0; background: transparent; border-radius: 8px; padding: 7px 0; font-weight: 500; }
.tool[data-tool="scan"] .tabs button.active { background: var(--panel); color: var(--text); box-shadow: 0 2px 6px rgba(0,0,0,.12); font-weight: 600; }
.scan-look button[data-filter] { border-radius: 999px; padding: 8px 14px; }
.scan-look button[data-filter].active { background: var(--accent); color: #ffffff; border-color: var(--accent); }
.scan-size { border-top: 0; background: var(--panel); border-radius: 14px; padding: 12px 14px; }

@media (max-width: 600px) {
    main { padding: 12px 16px 32px; }
    .tool .row > label:first-child, .tool .row > .label { min-width: 100%; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; }
}
.tools-home .card .card-text span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
