:root {
  --accent: #3b82f6;
  --bg: #0f1117;
  --bg2: #161925;
  --card: rgba(28, 32, 46, .78);
  --card-hover: rgba(38, 43, 62, .92);
  --line: rgba(255, 255, 255, .08);
  --text: #e8eaf2;
  --muted: #949ab0;
  --up: #34d399;
  --warn: #fbbf24;
  --down: #f87171;
  --radius: 14px;
  --tile-min: 210px;
  --tile-pad: 14px;
  --ico: 44px;
  --row: 74px;
  color-scheme: dark;
}
html[data-theme="light"] {
  --bg: #eef0f6;
  --bg2: #e3e6ef;
  --card: rgba(255, 255, 255, .86);
  --card-hover: #fff;
  --line: rgba(15, 20, 40, .09);
  --text: #171a26;
  --muted: #5d6378;
  color-scheme: light;
}
html[data-density="compact"] { --tile-min: 170px; --tile-pad: 10px; --ico: 34px; --row: 58px; }
html[data-density="large"]   { --tile-min: 250px; --tile-pad: 18px; --ico: 54px; --row: 90px; }

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: radial-gradient(1200px 700px at 10% -10%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 60%),
              radial-gradient(900px 600px at 100% 0%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 60%),
              var(--bg);
  background-attachment: fixed;
}
body.has-bg {
  background: linear-gradient(rgba(8, 10, 16, .55), rgba(8, 10, 16, .55)), var(--bg-img) center / cover fixed, var(--bg);
}
html[data-theme="light"] body.has-bg {
  background: linear-gradient(rgba(240, 242, 248, .55), rgba(240, 242, 248, .55)), var(--bg-img) center / cover fixed, var(--bg);
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.grow { flex: 1 1 auto; }

/* buttons */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 550; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--card-hover); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--down); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); cursor: pointer; display: grid; place-items: center;
}
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }
.icon-btn:hover, .icon-btn.on { background: var(--accent); color: #fff; border-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card {
  width: min(380px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 20px;
  padding: 30px; display: flex; flex-direction: column; gap: 14px; backdrop-filter: blur(14px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card p { margin: 0; }
.auth-logo { width: 54px; height: 54px; }
.msg { color: var(--down); min-height: 1.2em; margin: 0; font-size: 13.5px; }
.msg.ok { color: var(--up); }

/* forms */
label { display: flex; flex-direction: column; gap: 5px; font-size: 13.5px; font-weight: 550; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 11px; width: 100%;
}
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); }
input[type="color"] { padding: 2px; height: 40px; width: 70px; }
fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px 14px; margin: 0; display: flex; flex-direction: column; gap: 12px; }
legend { padding: 0 6px; font-weight: 650; color: var(--muted); font-size: 12.5px; text-transform: uppercase; letter-spacing: .06em; }
.row { display: flex; gap: 12px; align-items: flex-end; }
.row.wrap { flex-wrap: wrap; }
.file-btn { flex-direction: row; }

/* app header */
#app { max-width: 1500px; margin: 0 auto; padding: 18px clamp(12px, 3vw, 34px) 60px; }
.banner { background: color-mix(in srgb, var(--warn) 20%, var(--card)); border: 1px solid var(--line); padding: 8px 14px; border-radius: 10px; margin-bottom: 12px; font-size: 13.5px; }
.top { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; margin-bottom: 18px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { margin: 0; font-size: 21px; letter-spacing: -.01em; }
.search {
  flex: 1 1 320px; position: relative; display: flex; align-items: center; max-width: 640px; margin: 0 auto;
}
.search svg { position: absolute; z-index: 1; left: 13px; width: 18px; height: 18px; fill: var(--muted); pointer-events: none; }
.search input { padding: 11px 120px 11px 40px; border-radius: 13px; background: var(--card); backdrop-filter: blur(12px); font-size: 15px; }
.search-hint { position: absolute; right: 12px; font-size: 12px; color: var(--muted); pointer-events: none; max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-right { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.clock { display: flex; flex-direction: column; align-items: flex-end; margin-right: 6px; line-height: 1.15; }
.clock b { font-size: 20px; font-variant-numeric: tabular-nums; font-weight: 650; }

/* blocks grid */
.board { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-auto-rows: var(--row); gap: 12px; position: relative; min-height: var(--row); }
.board.editing:not(.flow) {
  border-radius: 14px; outline: 1px dashed var(--line); outline-offset: 6px; cursor: copy;
  background-image:
    linear-gradient(to right, color-mix(in srgb, var(--text) 6%, transparent) 1px, transparent 1px),
    linear-gradient(to bottom, color-mix(in srgb, var(--text) 6%, transparent) 1px, transparent 1px);
  background-size: calc((100% + 12px) / 12) calc(var(--row) + 12px);
  background-position: -6px -6px;
}
.board.flow { grid-template-columns: repeat(auto-fill, minmax(var(--tile-min), 1fr)); grid-auto-rows: minmax(var(--row), auto); grid-auto-flow: row dense; }
.item { position: relative; min-width: 0; min-height: 0; }
.placeholder { border: 2px dashed var(--accent); border-radius: var(--radius); background: color-mix(in srgb, var(--accent) 16%, transparent); pointer-events: none; z-index: 0; }
.ghost { position: fixed !important; left: 0; top: 0; z-index: 100; pointer-events: none; opacity: .92; box-shadow: 0 18px 50px rgba(0,0,0,.45); border-color: var(--accent) !important; }
.item.dragging { opacity: .3; }
.item.resizing { z-index: 3; outline: 2px solid var(--accent); }
.is-dragging, .is-dragging * { cursor: grabbing !important; user-select: none; }
.is-resizing, .is-resizing * { cursor: nwse-resize !important; user-select: none; }
.editing:not(.flow) .item { cursor: grab; touch-action: none; }
.rs { position: absolute; right: 3px; bottom: 3px; width: 18px; height: 18px; cursor: nwse-resize; z-index: 2; border-radius: 0 0 10px 0;
  background: linear-gradient(135deg, transparent 50%, var(--accent) 50%); opacity: .75; }
.rs:hover { opacity: 1; }
.w-edit { position: absolute; top: 8px; right: 8px; z-index: 2; border: 1px solid var(--line); background: var(--bg2); color: var(--text); border-radius: 8px; width: 28px; height: 28px; cursor: pointer; }
.flow-note { grid-column: 1 / -1; margin: 0; }

/* weather block */
.widget { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; backdrop-filter: blur(12px); overflow: hidden; display: flex; flex-direction: column; justify-content: center; }
.widget h3 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wx-now { display: flex; align-items: center; gap: 12px; min-width: 0; }
.wx-now .big { font-size: 32px; font-weight: 650; line-height: 1; }
.wx-now .emoji { font-size: 30px; }
.wx-label { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wx-days { display: flex; justify-content: space-between; gap: 6px; margin-top: 12px; font-size: 12.5px; text-align: center; }
.wx-days div { flex: 1; min-width: 0; }
.wx-ico { font-size: 18px; margin: 2px 0; }
.weather.flow-item { grid-column: 1 / -1; }

/* app tiles */
.tile {
  position: relative; display: flex; align-items: center; gap: 12px; padding: var(--tile-pad);
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  color: inherit; text-decoration: none; backdrop-filter: blur(12px); overflow: hidden;
  transition: transform .12s ease, background .12s ease, border-color .12s ease, box-shadow .12s ease;
}
.tile:hover { background: var(--card-hover); transform: translateY(-2px); border-color: color-mix(in srgb, var(--accent) 50%, transparent); box-shadow: 0 8px 26px rgba(0, 0, 0, .22); }
.tile .ico {
  width: var(--ico); height: var(--ico); flex: 0 0 var(--ico); border-radius: 11px; display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 14%, var(--bg2)); overflow: hidden; font-size: calc(var(--ico) * .55);
}
.tile .ico img { width: 78%; height: 78%; object-fit: contain; pointer-events: none; }
.tile .ico .letter { font-weight: 700; color: #fff; width: 100%; height: 100%; display: grid; place-items: center; font-size: calc(var(--ico) * .45); }
.tile .txt { min-width: 0; display: flex; flex-direction: column; }
.tile .txt b { font-weight: 620; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .txt small { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { position: absolute; top: 9px; right: 9px; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); opacity: .45; }
.dot.up { background: var(--up); opacity: 1; box-shadow: 0 0 0 3px color-mix(in srgb, var(--up) 22%, transparent); }
.dot.warn { background: var(--warn); opacity: 1; }
.dot.down { background: var(--down); opacity: 1; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--down) 25%, transparent); } }
.editing .tile:hover, .editing .widget:hover { transform: none; }
.tile.mini .dot { top: 6px; right: 6px; width: 7px; height: 7px; }
.tile { height: 100%; }
.tile.tall { flex-direction: column; justify-content: center; text-align: center; gap: 10px; }
.tile.tall .ico { width: 64px; height: 64px; flex-basis: 64px; font-size: 36px; }
.tile.tall .txt small { white-space: normal; }
.tile.mini { flex-direction: column; justify-content: center; gap: 4px; padding: 8px 6px; text-align: center; }
.tile.mini .ico { width: 34px; height: 34px; flex-basis: 34px; font-size: 20px; }
.tile.mini .txt b { font-size: 12.5px; }
.tile.mini .txt small { display: none; }
.empty { text-align: center; padding: 40px 0; }

/* board */
.editbar {
  position: sticky; top: 8px; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: color-mix(in srgb, var(--accent) 18%, var(--card)); border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  border-radius: 12px; padding: 10px 14px; margin-bottom: 18px; backdrop-filter: blur(14px); font-size: 14px;
}
.editbar div { display: flex; gap: 8px; flex-wrap: wrap; }
.editbar { margin-bottom: 22px; }
.empty { text-align: center; padding: 40px 0; }

/* dialogs */
dialog { border: 1px solid var(--line); background: var(--bg); color: var(--text); border-radius: 18px; padding: 0; width: min(620px, calc(100vw - 24px)); max-height: calc(100vh - 30px); box-shadow: 0 30px 90px rgba(0, 0, 0, .5); }
dialog::backdrop { background: rgba(4, 6, 12, .6); backdrop-filter: blur(3px); }
.dlg { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.dlg.narrow { width: auto; }
#secDlg { width: min(420px, calc(100vw - 24px)); }
.dlg h2 { margin: 0 0 2px; font-size: 19px; }
.dlg-actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.icon-preview { width: 58px; height: 58px; flex: 0 0 58px; border-radius: 14px; display: grid; place-items: center; background: color-mix(in srgb, var(--accent) 14%, var(--bg2)); overflow: hidden; font-size: 30px; }
.icon-preview img { width: 78%; height: 78%; object-fit: contain; }
.icon-preview .letter { width: 100%; height: 100%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 26px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-weight: 600; font-size: 14px; z-index: 50; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); }

@media (max-width: 720px) {
  .top { gap: 12px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .search input { padding-right: 14px; }
  .search-hint { display: none; }
  .clock b { font-size: 17px; }
  :root { --tile-min: 150px; }
  .tile .txt small { display: none; }
}
