/* Cnnote - shared styles for login, desktop, mobile, scanner and tuner. */

:root {
    --brand: #14213d;
    --accent: #2f6fed;
    --accent-ink: #ffffff;
    --green: #7ae582;
    --danger: #d64545;
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #eef0f5;
    --text: #1b1f2a;
    --muted: #667085;
    --border: #dde1ea;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
    --radius: 14px;
    --tile-dark: #1e2636;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #11141b;
        --surface: #1a1e28;
        --surface-2: #232937;
        --text: #e8eaf0;
        --muted: #98a2b3;
        --border: #2c3344;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 12px rgba(0, 0, 0, .25);
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ------------------------------------------------------------ basics */

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.brand img { border-radius: 8px; display: block; }
.brand-lg { font-size: 26px; justify-content: center; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    min-height: 42px; padding: 0 18px; border-radius: 10px; border: 1px solid transparent;
    background: var(--surface-2); color: var(--text); cursor: pointer; font-weight: 600;
    text-decoration: none; white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-dark { background: rgba(255, 255, 255, .14); color: #fff; }
.btn-sm { min-height: 32px; padding: 0 12px; font-size: 14px; }
.btn-block { width: 100%; }
.btn-danger { background: var(--danger); color: #fff; }

.icon-btn {
    display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 50%;
    border: 0; background: transparent; cursor: pointer; font-size: 20px; line-height: 1;
    color: inherit; text-decoration: none; list-style: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.danger { color: var(--danger); }
.icon-btn.on-dark { color: #fff; background: rgba(0, 0, 0, .35); }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 6px 10px; cursor: pointer; font-size: 16px; line-height: 1; }
.seg button.active { background: var(--accent); color: #fff; }

.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .search, .title-input {
    width: 100%; min-height: 44px; padding: 0 12px; border-radius: 10px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.field input:focus, .search:focus, .title-input:focus, .viewer-title:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.alert { background: #fdecec; color: #8a1c1c; border-radius: 10px; padding: 10px 12px; margin-bottom: 14px; font-size: 14px; }

.toast {
    position: fixed; left: 50%; bottom: calc(110px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
    background: #1b1f2a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
    opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 1000;
    max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-error { background: var(--danger); }
body.desktop .toast { bottom: 32px; }

/* transparency / preview backgrounds */
.bg-light { background: #ffffff; }
.bg-dark { background: #1e2636; }
.bg-checker {
    background-color: #fff;
    background-image: conic-gradient(#e3e6ec 25%, transparent 0 50%, #e3e6ec 0 75%, transparent 0);
    background-size: 20px 20px;
}

/* ------------------------------------------------------------ login */

body.login { min-height: 100vh; background: var(--brand); }
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
    width: 100%; max-width: 380px; background: var(--surface); border-radius: 18px;
    padding: 28px 24px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
.login-sub { text-align: center; color: var(--muted); margin: 6px 0 22px; font-size: 14px; }

/* ------------------------------------------------------------ notes grid */

.content { padding: 16px; }
.content-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.content-head h1 { margin: 0; font-size: 22px; }
.count { font-size: 14px; color: var(--muted); font-weight: 500; margin-left: 4px; }
.hint { margin: 0; color: var(--muted); font-size: 14px; }

.note-grid {
    list-style: none; margin: 0; padding: 0; display: grid; gap: 12px;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.note-card { min-width: 0; }
.note-open {
    display: flex; flex-direction: column; width: 100%; padding: 0; border: 1px solid var(--border);
    border-radius: var(--radius); background: var(--surface); cursor: pointer; overflow: hidden;
    text-align: left; box-shadow: var(--shadow); transition: transform .12s, box-shadow .12s;
}
.note-open:hover { transform: translateY(-2px); }
.note-open:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.note-thumb { display: block; aspect-ratio: 4 / 3; background: #fff; padding: 8px; border-bottom: 1px solid var(--border); }
.tone-light .note-thumb { background: var(--tile-dark); }
.note-thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.note-meta { display: block; padding: 8px 10px 10px; }
.note-title { display: block; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-sub { display: block; font-size: 12px; color: var(--muted); }

.empty-state { text-align: center; color: var(--muted); padding: 60px 16px; max-width: 420px; margin: 0 auto; }
.empty-icon { font-size: 48px; opacity: .5; }

/* ------------------------------------------------------------ desktop */

.topbar {
    position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 20px;
    padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar .search { max-width: 420px; flex: 1; min-height: 40px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.topbar-right form { margin: 0; }
.user-chip { font-size: 13px; color: var(--muted); }
.link { font-size: 14px; font-weight: 600; text-decoration: none; }
body.desktop .content { max-width: 1400px; margin: 0 auto; padding: 24px; }
body.desktop .note-grid { grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 18px; }

@media (max-width: 700px) {
    .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
    .topbar .search { order: 3; max-width: none; flex-basis: 100%; }
    .user-chip { display: none; }
    body.desktop .content { padding: 16px; }
}

/* ------------------------------------------------------------ mobile */

body.mobile { padding-bottom: calc(120px + env(safe-area-inset-bottom)); }
.appbar {
    position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
    padding: calc(8px + env(safe-area-inset-top)) 8px 8px 16px; background: var(--brand); color: #fff;
}
.appbar-right { display: flex; align-items: center; gap: 2px; }
.appbar .icon-btn:hover { background: rgba(255, 255, 255, .12); }
.appbar .btn-ghost { color: #fff; border-color: rgba(255, 255, 255, .4); }
.search-row { padding: 10px 16px 0; }

.menu { position: relative; }
.menu summary::-webkit-details-marker { display: none; }
.menu-pop {
    position: absolute; right: 0; top: 46px; min-width: 220px; background: var(--surface); color: var(--text);
    border-radius: 12px; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); padding: 6px; z-index: 20;
}
.menu-pop a, .menu-pop button {
    display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; background: none;
    border-radius: 8px; color: var(--text); text-decoration: none; cursor: pointer;
}
.menu-pop a:hover, .menu-pop button:hover { background: var(--surface-2); }
.menu-pop form { margin: 0; }
.menu-user { font-size: 12px; color: var(--muted); padding: 8px 12px; border-bottom: 1px solid var(--border); margin-bottom: 4px; word-break: break-all; }

.scan-fab {
    position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); transform: translateX(-50%);
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    width: 84px; height: 84px; border-radius: 50%; background: var(--accent); color: #fff;
    text-decoration: none; font-weight: 700; font-size: 13px; z-index: 30;
    box-shadow: 0 8px 24px rgba(47, 111, 237, .45), 0 0 0 6px var(--bg);
}
.scan-fab:active { transform: translateX(-50%) scale(.96); }

/* ------------------------------------------------------------ viewer */

.viewer {
    width: 100vw; height: 100vh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0;
    background: var(--surface); color: var(--text);
}
.viewer[open] { display: flex; flex-direction: column; }
.viewer::backdrop { background: rgba(0, 0, 0, .6); }
.viewer-bar {
    display: flex; align-items: center; gap: 8px; padding: calc(8px + env(safe-area-inset-top)) 8px 8px;
    border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.viewer-title {
    flex: 1; min-width: 140px; min-height: 40px; border: 1px solid transparent; border-radius: 8px;
    background: transparent; padding: 0 10px; font-weight: 600; font-size: 16px;
}
.viewer-title:hover { border-color: var(--border); }
.viewer-actions { display: flex; align-items: center; gap: 4px; }
.viewer-stage {
    flex: 1; min-height: 0; display: grid; place-items: center; padding: 16px; overflow: auto;
    grid-template: minmax(0, 1fr) / minmax(0, 1fr);
}
.viewer-stage img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.viewer-info { padding: 8px 16px calc(8px + env(safe-area-inset-bottom)); font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); }

@media (max-width: 600px) {
    .viewer-title { order: 3; flex-basis: 100%; }
    .viewer-actions { margin-left: auto; }
}

/* ------------------------------------------------------------ scanner */

body.scanner-page { background: #000; color: #fff; overflow: hidden; overscroll-behavior: none; }
.scanner {
    position: fixed; inset: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; grid-template-columns: minmax(0, 1fr);
    background: #000; user-select: none; -webkit-user-select: none;
}
.stage { grid-row: 2; grid-column: 1; display: none; position: relative; min-height: 0; }
.scanner[data-state="camera"] .stage-camera,
.scanner[data-state="crop"] .stage-crop,
.scanner[data-state="result"] .stage-result { display: block; }

.stage-camera video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.cam-msg {
    position: absolute; left: 16px; right: 16px; top: 50%; transform: translateY(-50%);
    background: rgba(255, 255, 255, .1); border-radius: 12px; padding: 16px; text-align: center; font-size: 15px;
}
.stage-crop canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.stage-result { padding: 12px; }
.result-view {
    width: 100%; height: 100%; border-radius: 10px; display: grid; place-items: center; overflow: hidden;
    grid-template: minmax(0, 1fr) / minmax(0, 1fr);
}
.result-view canvas { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.result-empty {
    position: absolute; left: 24px; right: 24px; top: 50%; transform: translateY(-50%);
    background: rgba(0, 0, 0, .75); padding: 14px; border-radius: 12px; text-align: center; font-size: 14px;
}

.busy {
    position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 14px; background: rgba(0, 0, 0, .45); font-weight: 600;
}
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid rgba(255, 255, 255, .25); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.scan-top {
    grid-row: 1; display: flex; align-items: center; gap: 10px;
    padding: calc(10px + env(safe-area-inset-top)) 12px 10px; background: #0b0f17;
}
.preset-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; flex: 1; }
.preset-chips::-webkit-scrollbar { display: none; }
.preset-chips button, .ink-chips button {
    flex: none; border: 1px solid rgba(255, 255, 255, .25); background: transparent; color: #fff;
    padding: 7px 12px; border-radius: 999px; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.preset-chips button.active, .ink-chips button.active { background: #fff; color: #0b0f17; border-color: #fff; }

.scan-bottom { grid-row: 3; background: #0b0f17; padding: 12px 16px calc(14px + env(safe-area-inset-bottom)); }
.controls { display: none; }
.scanner[data-state="camera"] .controls-camera,
.scanner[data-state="crop"] .controls-crop,
.scanner[data-state="result"] .controls-result { display: block; }
.scanner[data-state="camera"] .controls-camera { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; }

.round-btn {
    width: 52px; height: 52px; border-radius: 50%; border: 0; display: grid; place-items: center;
    background: rgba(255, 255, 255, .14); color: #fff; cursor: pointer;
}
.round-btn.active { background: #ffd166; color: #000; }
.round-btn-spacer { width: 52px; height: 52px; }
.shutter {
    width: 78px; height: 78px; border-radius: 50%; border: 5px solid #fff; background: #fff;
    box-shadow: inset 0 0 0 4px #0b0f17; cursor: pointer;
}
.shutter:active { transform: scale(.94); }
.shutter:disabled { opacity: .4; }

.crop-hint { margin: 0 0 10px; text-align: center; font-size: 14px; color: rgba(255, 255, 255, .75); }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

.slider-row { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.slider-row label { width: 86px; font-size: 13px; color: rgba(255, 255, 255, .8); }
.slider-row input { flex: 1; accent-color: var(--green); }
.chip-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.ink-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.ink-chips button { padding: 6px 10px; font-size: 13px; display: inline-flex; align-items: center; gap: 6px; }
.ink-chips i { width: 12px; height: 12px; border-radius: 50%; display: inline-block; border: 1px solid rgba(127, 127, 127, .6); }
.seg-dark { border-color: rgba(255, 255, 255, .25); flex: none; }
.seg-dark button { color: #fff; }
.seg-dark button.active { background: #fff; color: #000; }
.title-input { background: rgba(255, 255, 255, .08); border-color: rgba(255, 255, 255, .2); color: #fff; margin-bottom: 10px; }
.title-input::placeholder { color: rgba(255, 255, 255, .5); }

/* ------------------------------------------------------------ tuner */

body.tuner { min-height: 100vh; }
.tuner-layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); min-height: calc(100vh - 65px); }
.tuner-side { border-right: 1px solid var(--border); background: var(--surface); padding: 16px; overflow-y: auto; max-height: calc(100vh - 65px); position: sticky; top: 65px; }
.tuner-side h2 { font-size: 15px; margin: 18px 0 8px; }
.tuner-side h2:first-child { margin-top: 0; }
.param { margin-bottom: 12px; }
.param-head { display: flex; justify-content: space-between; font-size: 13px; font-weight: 600; }
.param-head output { font-variant-numeric: tabular-nums; color: var(--accent); }
.param input[type="range"] { width: 100%; }
.param select { width: 100%; min-height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); padding: 0 8px; }
.param .help { font-size: 12px; color: var(--muted); margin: 2px 0 0; }
.param.changed .param-head label::after { content: " •"; color: var(--accent); }
.tuner-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.tuner-actions .btn { min-height: 36px; padding: 0 12px; font-size: 14px; }
.tuner-main { padding: 16px 20px; min-width: 0; }
.tuner-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.tuner-toolbar .search { max-width: 280px; min-height: 38px; }
.dropzone {
    border: 2px dashed var(--border); border-radius: var(--radius); padding: 40px 16px; text-align: center; color: var(--muted);
}
.dropzone.over { border-color: var(--accent); color: var(--accent); }
.tuner-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 16px; }
.tcard { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tcard-imgs { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.tcard-imgs > div {
    aspect-ratio: 4 / 3; display: grid; place-items: center; padding: 6px; overflow: hidden;
    grid-template: minmax(0, 1fr) / minmax(0, 1fr);
}
.tcard-imgs img, .tcard-imgs canvas { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.tcard-imgs .orig { background: #000; }
.tcard-foot { display: flex; justify-content: space-between; gap: 8px; padding: 8px 10px; font-size: 12px; border-top: 1px solid var(--border); }
.tcard-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tcard-stats { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.tuner-status { font-size: 13px; color: var(--muted); }

@media (max-width: 900px) {
    .tuner-layout { grid-template-columns: 1fr; }
    .tuner-side { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--border); }
    .tuner-grid { grid-template-columns: 1fr; }
}
