/* Shared UI parts driven by ui.js (each one a progressive enhancement; pages work without JS):
   command palette (⌘K), toasts, the confirm dialog, sortable tables, avatars, the drop zone for
   files, meter tones on .bar, and [data-tip] tooltips. Colours are app.css tokens; theme-specific
   values go under [data-theme]. Linked after app.css in base.html. */
:root, [data-theme="dark"] {
  --overlay: rgba(0, 0, 0, .55); --pop-shadow: 0 16px 48px rgba(0, 0, 0, .5);
  --av-0: #3b5a78; --av-1: #5a4a7a; --av-2: #2f6a5a; --av-3: #7a4e3a; --av-4: #6a3f5c; --av-5: #4a6a32; --av-6: #2f5f74; --av-7: #6e5a2c;
  --av-ink: #f2f2f6;
}
[data-theme="light"] {
  --overlay: rgba(16, 34, 68, .35); --pop-shadow: 0 16px 48px rgba(16, 34, 68, .18);
  --av-0: #cfe1f1; --av-1: #e0d6f3; --av-2: #cdeee2; --av-3: #f6dccd; --av-4: #f1d3e6; --av-5: #dcefc9; --av-6: #cbe8f2; --av-7: #f3e6c4;
  --av-ink: #102244;
}

/* ---------------------------------------------------------------- keyboard hint button (header) */
.kbdbtn { font-family: var(--font-mono); font-size: .8rem; color: var(--muted); padding: .5rem .7rem; gap: .2rem; }
.kbdbtn:hover { color: var(--ink); }
@media (max-width: 899px) { .kbdbtn { display: none; } }
kbd { font-family: var(--font-mono); font-size: .72rem; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 .3rem; color: var(--muted); background: var(--bg); }

/* ---------------------------------------------------------------- dialogs (palette + confirm) */
dialog.uidlg { border: 1px solid var(--line); border-radius: 12px; background: var(--card); color: var(--ink); padding: 0;
  box-shadow: var(--pop-shadow); max-width: calc(100vw - 1.5rem); max-height: calc(100dvh - 2rem); overflow: hidden; }
dialog.uidlg::backdrop { background: var(--overlay); }
dialog.uidlg[open] { animation: ui-pop .16s ease-out; }
@keyframes ui-pop { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }

/* command palette */
dialog.cmdk { width: 600px; margin: 12vh auto auto; display: none; flex-direction: column; }
dialog.cmdk[open] { display: flex; }
.cmdk-in { display: flex; align-items: center; gap: .5rem; padding: .4rem .8rem; border-bottom: 1px solid var(--line); }
dialog.cmdk .cmdk-in input[type=text] { border: 0; background: transparent; font-size: 1.05rem; padding: .6rem 0; outline: none; min-width: 0; }
.cmdk-in .cmdk-ico { color: var(--muted); flex: none; width: 18px; height: 18px; border: 2px solid currentColor; border-radius: 50%; position: relative; }
.cmdk-in .cmdk-ico::after { content: ""; position: absolute; width: 2px; height: 7px; background: currentColor; right: -4px; bottom: -6px; transform: rotate(-45deg); }
.cmdk-list { list-style: none; margin: 0; padding: .3rem; overflow-y: auto; max-height: min(420px, 60dvh); overscroll-behavior: contain; }
.cmdk-list .grp { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; padding: .55rem .6rem .25rem; }
.cmdk-list .opt { display: flex; align-items: baseline; gap: .6rem; min-height: 44px; padding: .55rem .6rem; border-radius: 8px; cursor: pointer; }
.cmdk-list .opt b { font-weight: 500; white-space: nowrap; }
.cmdk-list .opt span { color: var(--muted); font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.cmdk-list .opt[aria-selected="true"] { background: var(--chip); box-shadow: inset 2px 0 0 var(--accent); }
.cmdk-list .none { padding: 1rem .6rem; color: var(--muted); }
.cmdk-foot { display: flex; gap: 1rem; padding: .45rem .8rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .75rem; }
.cmdk-foot span { display: inline-flex; gap: .3rem; align-items: center; }
@media (max-width: 640px) {
  dialog.cmdk { margin: .5rem auto auto; width: calc(100vw - 1rem); max-width: calc(100vw - 1rem); }
  .cmdk-foot { display: none; }
}

/* confirm */
dialog.confirm { width: 400px; }
.confirm-body { padding: 1.1rem 1.2rem .4rem; }
.confirm-body h2 { margin: 0; font-size: 1.05rem; overflow-wrap: anywhere; }
.confirm-foot { display: flex; justify-content: flex-end; gap: .5rem; padding: 1rem 1.2rem 1.1rem; }
.btn.danger { background: var(--err); border-color: var(--err); color: var(--accent-ink); font-weight: 600; }
[data-theme="light"] .btn.danger { color: #fff; }
@media (max-width: 480px) { .confirm-foot { flex-direction: column-reverse; } .confirm-foot .btn { width: 100%; } }

/* ---------------------------------------------------------------- toasts */
.toasts { position: fixed; z-index: 70; display: flex; flex-direction: column; gap: .5rem; pointer-events: none;
  left: .75rem; right: .75rem; bottom: calc(72px + env(safe-area-inset-bottom)); align-items: center; }
@media (min-width: 900px) { .toasts { left: auto; right: 1rem; top: 68px; bottom: auto; align-items: flex-end; width: 380px; } }
.toast { pointer-events: auto; position: relative; display: flex; align-items: center; gap: .4rem; width: 100%; max-width: 420px;
  background: var(--card); color: var(--ink); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 10px;
  padding: .35rem .35rem .35rem .85rem; box-shadow: var(--pop-shadow); overflow: hidden; animation: ui-toast-in .2s ease-out; }
.toast.ok { border-left-color: var(--ok); } .toast.err { border-left-color: var(--err); } .toast.warn { border-left-color: var(--warn); }
.toast .msg { flex: 1; min-width: 0; overflow-wrap: anywhere; padding: .45rem 0; }
.toast .act { flex: none; }
.toast .x { flex: none; width: 44px; height: 44px; border: 0; background: none; color: var(--muted); cursor: pointer; border-radius: 8px; font-size: 1rem; }
.toast .x:hover { color: var(--ink); background: var(--chip); }
.toast .tick { position: absolute; left: 0; bottom: 0; height: 2px; width: 100%; background: currentColor; opacity: .25; transform-origin: left; }
.toast.ok .tick { color: var(--ok); } .toast.err .tick { color: var(--err); }
.toast.out { opacity: 0; transform: translateY(6px); transition: opacity .2s, transform .2s; }
@keyframes ui-toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
/* the server flash shows as a toast when JS runs; #flash stays as the no-JS fallback */
.js .flash[data-toast] { display: none; }

/* ---------------------------------------------------------------- sortable tables */
table.list thead th, table.list tr.headrow th { position: sticky; top: var(--sticky-top, 0px); background: var(--card); z-index: 1; box-shadow: inset 0 -1px 0 var(--line); }
th .sortbtn { all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: .3rem; min-height: 32px; color: inherit; font: inherit; letter-spacing: inherit; }
th.num .sortbtn { flex-direction: row-reverse; }
th .sortbtn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
th .sortbtn::after { content: ""; width: 0; height: 0; border: 4px solid transparent; border-top-color: currentColor; margin-top: 4px; opacity: 0; transition: opacity .1s; }
th .sortbtn:hover::after { opacity: .5; }
th[aria-sort="ascending"] .sortbtn::after { opacity: 1; border-top-color: transparent; border-bottom-color: currentColor; margin: -4px 0 0; }
th[aria-sort="descending"] .sortbtn::after { opacity: 1; }
th[aria-sort] { color: var(--ink); }
@media (max-width: 899px) { th .sortbtn { min-height: 40px; } }

/* ---------------------------------------------------------------- avatars */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; width: 24px; height: 24px; border-radius: 50%;
  font: 600 .66rem/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; letter-spacing: .02em; color: var(--av-ink); vertical-align: middle; user-select: none; }
.avatar.av-0 { background: var(--av-0); } .avatar.av-1 { background: var(--av-1); } .avatar.av-2 { background: var(--av-2); } .avatar.av-3 { background: var(--av-3); }
.avatar.av-4 { background: var(--av-4); } .avatar.av-5 { background: var(--av-5); } .avatar.av-6 { background: var(--av-6); } .avatar.av-7 { background: var(--av-7); }
.usermenu > summary .avatar { width: 28px; height: 28px; font-size: .72rem; }
@media (max-width: 640px) { .usermenu > summary .uname { display: none; } }
td.who { white-space: nowrap; } td.who .avatar { margin-right: .35rem; }

/* ---------------------------------------------------------------- files drop zone */
.dz { display: block; margin: 0; }
.js .dz { position: relative; display: flex; align-items: center; justify-content: center; min-height: 64px; padding: .6rem;
  border: 1.5px dashed var(--line); border-radius: 10px; color: var(--muted); cursor: pointer; text-align: center; transition: border-color .15s, background .15s; }
.js .dz:hover, .js .dz:focus-within { border-color: var(--accent); color: var(--ink); }
.js .dz.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); color: var(--ink); }
.js .dz input[type=file] { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; min-height: 0; }
.dz-hint { display: none; } .js .dz-hint { display: inline; overflow-wrap: anywhere; }
.js .dz:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.dz-list { list-style: none; padding: 0; margin: .3rem 0; display: grid; gap: .35rem; }
.dz-list li { display: grid; grid-template-columns: 1fr auto; gap: .15rem .6rem; font-size: .85rem; }
.dz-list li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.dz-list li .bar { grid-column: 1 / -1; }
.dz-list li.err { color: var(--err); }

/* ---------------------------------------------------------------- meter tone (.bar) */
.bar i { transition: width .3s ease, background-color .3s; }
.bar[data-tone="low"] i { background: var(--muted); }
.bar[data-tone="mid"] i { background: var(--accent); }
.bar[data-tone="done"] i { background: var(--ok); }
.bar[data-flagged] i { background: var(--err); }

/* ---------------------------------------------------------------- tooltips */
.tip { position: fixed; z-index: 90; max-width: min(260px, calc(100vw - 16px)); padding: .3rem .55rem; border-radius: 6px; pointer-events: none;
  background: var(--ink); color: var(--bg); font-size: .78rem; line-height: 1.3; opacity: 0; transition: opacity .12s; }
.tip.on { opacity: 1; }
.tip::after { content: ""; position: absolute; left: var(--tip-x, 50%); width: 8px; height: 8px; background: inherit; transform: translateX(-50%) rotate(45deg); bottom: -4px; }
.tip.below::after { bottom: auto; top: -4px; }

@media (prefers-reduced-motion: reduce) {
  dialog.uidlg[open], .toast { animation: none; }
  .toast.out, .bar i, .tip, .js .dz { transition: none; }
}
@media print { .toasts, .tip, dialog.uidlg { display: none !important; } }
