:root {
    color-scheme: light dark;
    --bg: #eef0f5;
    --surface: #ffffff;
    --surface-2: #f6f7fa;
    --border: #e1e4ec;
    --text: #1c2030;
    --muted: #6b7288;
    --accent: #6d4aff;
    --accent-text: #ffffff;
    --accent-soft: #ece8ff;
    --danger: #d6324a;
    --radius: 12px;
    --shadow: 0 6px 24px rgba(20, 24, 50, .12);

    --c-yellow: #fff2b3;
    --c-green: #d3f3d0;
    --c-blue: #d2e7ff;
    --c-pink: #ffd8e6;
    --c-purple: #e4dbff;
    --c-orange: #ffe0c0;
    --c-gray: #e6e8ee;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f111a;
        --surface: #181b27;
        --surface-2: #1f2331;
        --border: #2a2f42;
        --text: #e8eaf3;
        --muted: #8b92aa;
        --accent: #8b6dff;
        --accent-text: #0f111a;
        --accent-soft: #2b2650;
        --danger: #ff6b81;
        --shadow: 0 6px 24px rgba(0, 0, 0, .5);

        --c-yellow: #4b4420;
        --c-green: #24422a;
        --c-blue: #21405f;
        --c-pink: #5a2b3e;
        --c-purple: #3e3463;
        --c-orange: #5a3a1f;
        --c-gray: #353a47;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* kolory notatek: ustawiaja --note-bg */
.c-yellow { --note-bg: var(--c-yellow); }
.c-green { --note-bg: var(--c-green); }
.c-blue { --note-bg: var(--c-blue); }
.c-pink { --note-bg: var(--c-pink); }
.c-purple { --note-bg: var(--c-purple); }
.c-orange { --note-bg: var(--c-orange); }
.c-gray { --note-bg: var(--c-gray); }

/* przyciski */
.btn {
    display: inline-flex; align-items: center; gap: 6px;
    border: 1px solid transparent; border-radius: 10px;
    padding: 8px 14px; cursor: pointer; background: var(--surface-2);
    font-weight: 600; white-space: nowrap;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.icon-btn {
    display: inline-grid; place-items: center; width: 36px; height: 36px; flex: none;
    border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn svg, .btn svg, .search svg { width: 18px; height: 18px; }
.nav-item > svg { width: 18px; height: 18px; flex: none; }

/* layout trzech paneli */
#app { display: grid; grid-template-columns: 240px 320px 1fr; height: 100%; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }
#sidebar { background: var(--surface-2); border-right: 1px solid var(--border); }
#listpane { border-right: 1px solid var(--border); }
.pane-head {
    display: flex; align-items: center; gap: 8px; padding: 12px 14px;
    padding-top: max(12px, env(safe-area-inset-top)); min-height: 60px;
}
.pane-head h2 { margin: 0; font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-foot { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.spacer { flex: 1; }
.only-mobile { display: none; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; flex: 1; }
.brand-logo, .login-logo {
    display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
    background: linear-gradient(135deg, var(--accent), #ff6bb5); color: #fff; font-weight: 800;
}

/* sidebar: grupy + tagi */
.nav-scroll, .notes-scroll { flex: 1; overflow-y: auto; padding: 4px 10px 16px; overscroll-behavior: contain; }
.nav-title { margin: 14px 8px 6px; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.nav-item {
    display: flex; align-items: center; gap: 8px; width: 100%; padding: 9px 10px;
    border: 0; border-radius: 10px; background: transparent; text-align: left; cursor: pointer;
}
.nav-item:hover { background: var(--border); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count { font-size: 12px; color: var(--muted); }
.nav-item .row-actions { display: none; gap: 2px; }
.nav-item:hover .row-actions, .nav-item.active .row-actions { display: inline-flex; }
.row-actions .icon-btn { width: 26px; height: 26px; }
.row-actions .icon-btn svg { width: 14px; height: 14px; }
@media (hover: none) { .nav-item .row-actions { display: inline-flex; } }

/* lista notatek */
.search { position: relative; padding: 0 14px 8px; }
.search svg { position: absolute; left: 25px; top: 10px; color: var(--muted); }
.search input {
    width: 100%; padding: 9px 12px 9px 36px; border: 1px solid var(--border); border-radius: 10px;
    background: var(--surface-2); outline: none;
}
.search input:focus { border-color: var(--accent); }

.note-card {
    position: relative; display: block; width: 100%; margin: 0 0 8px; padding: 11px 14px 11px 18px;
    border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2);
    text-align: left; cursor: pointer; overflow: hidden;
}
.note-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 6px; background: var(--note-bg, transparent); }
.note-card:hover { border-color: var(--accent); }
.note-card.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); background: var(--surface); }
.note-card .t { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-card .s { color: var(--muted); font-size: 13px; max-height: 2.9em; overflow: hidden; word-break: break-word; }
.note-card .d { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.chip {
    display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px;
    background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600;
}
.none { color: var(--muted); text-align: center; padding: 32px 12px; }

/* edytor */
#editor { background: var(--note-bg, var(--surface)); position: relative; transition: background .2s; }
#note-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; }
.editor-head { border-bottom: 1px solid color-mix(in srgb, var(--text) 10%, transparent); }
.save-status { font-size: 12px; color: var(--muted); }
.seg { display: inline-flex; background: color-mix(in srgb, var(--text) 8%, transparent); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 8px; cursor: pointer; font-size: 13px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .15); }
.editor-body { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 18px clamp(14px, 4vw, 48px); gap: 8px; overflow-y: auto; }
.title-input { border: 0; outline: none; background: transparent; font-size: 28px; font-weight: 750; width: 100%; padding: 4px 0; }
.meta-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.meta-row select {
    border: 1px solid color-mix(in srgb, var(--text) 15%, transparent); background: color-mix(in srgb, var(--surface) 70%, transparent);
    border-radius: 8px; padding: 4px 8px; font-size: 13px;
}
.tags { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; flex: 1; min-width: 160px; }
.tags .chip button { border: 0; background: transparent; cursor: pointer; padding: 0; line-height: 1; color: inherit; font-size: 14px; }
.tags input { border: 0; outline: none; background: transparent; min-width: 110px; font-size: 13px; padding: 4px 2px; }
.body-input {
    flex: 1; min-height: 260px; width: 100%; resize: none; border: 0; outline: none; background: transparent;
    font: 15px/1.65 ui-monospace, "SF Mono", Menlo, Consolas, monospace; padding: 8px 0;
}
.body-input.plain { font-family: inherit; font-size: 16px; }
.preview { flex: 1; padding: 4px 0 24px; }

/* wyrenderowany Markdown */
.markdown { line-height: 1.7; word-wrap: break-word; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.25; margin: 1.2em 0 .5em; }
.markdown p, .markdown ul, .markdown ol, .markdown blockquote, .markdown pre, .markdown table { margin: 0 0 1em; }
.markdown code { background: color-mix(in srgb, var(--text) 10%, transparent); padding: 1px 6px; border-radius: 6px; font-size: .9em; }
.markdown pre { background: color-mix(in srgb, var(--text) 9%, transparent); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.markdown pre code { background: none; padding: 0; }
.markdown blockquote { border-left: 4px solid var(--accent); margin-left: 0; padding: 0 14px; color: var(--muted); }
.markdown table { border-collapse: collapse; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 6px 12px; }
.markdown img { max-width: 100%; }
.markdown a { color: var(--accent); }
.markdown input[type=checkbox] { margin-right: 6px; }

/* popover z kolorami */
.popover {
    position: absolute; right: 56px; top: 56px; z-index: 5; display: flex; flex-wrap: wrap; gap: 8px; max-width: 220px;
    padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); cursor: pointer; background: var(--note-bg, var(--surface)); }
.swatch.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.swatch.none-color { background: repeating-linear-gradient(45deg, var(--surface), var(--surface) 4px, var(--border) 4px, var(--border) 6px); }

/* dialog */
dialog {
    border: 1px solid var(--border); border-radius: 16px; padding: 20px; width: min(380px, calc(100vw - 32px));
    background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(8, 10, 20, .5); backdrop-filter: blur(2px); }
dialog h3 { margin: 0 0 12px; }
dialog input[type=text], dialog input[type=password] {
    width: 100%; margin-bottom: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); outline: none;
}
dialog input:focus { border-color: var(--accent); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
dialog p { margin: 0 0 10px; color: var(--muted); }

/* logowanie */
.login-page { display: grid; place-items: center; padding: 16px; }
.login-card {
    width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; align-items: stretch;
    padding: 32px 28px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); text-align: center;
}
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-logo { width: 52px; height: 52px; font-size: 26px; margin: 0 auto 4px; border-radius: 15px; }
.login-card input { padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); outline: none; }
.login-card input:focus { border-color: var(--accent); }
.login-error { color: var(--danger); margin: 0; font-size: 14px; }

/* mobile: jeden panel naraz, wybierany przez #app[data-view] */
@media (max-width: 1000px) {
    #app { display: block; }
    .pane { position: absolute; inset: 0; width: 100%; height: 100%; }
    #app[data-view="groups"] > :not(#sidebar),
    #app[data-view="list"] > :not(#listpane),
    #app[data-view="note"] > :not(#editor) { display: none; }
    .only-mobile { display: inline-grid; }
    .popover { right: 12px; }
}
