:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --section: var(--tg-theme-section-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --link: var(--tg-theme-link-color, #2a7ae2);
  --accent: var(--tg-theme-button-color, #2a7ae2);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sep: var(--tg-theme-section-separator-color, rgba(127, 127, 127, 0.2));
  --top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --bottom: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
  color-scheme: light dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg2); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-top: var(--top);
  padding-bottom: calc(var(--bottom) + 16px);
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.top {
  position: sticky; top: var(--top); z-index: 2;
  background: var(--bg2); padding: 8px 16px 4px;
}
#search {
  width: 100%; border: 0; border-radius: 10px; padding: 9px 12px;
  background: var(--section); outline: none;
}

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips.inline { padding: 0; flex: 1; min-width: 0; }
.chip {
  flex: none; border: 0; border-radius: 16px; padding: 6px 12px;
  background: var(--section); white-space: nowrap; font-size: 14px;
}
.chip.on { background: var(--accent); color: var(--accent-text); }
.chip .n { opacity: 0.6; margin-left: 4px; }
.chip.x::after { content: "✕"; margin-left: 6px; opacity: 0.7; }

.toolbar { display: flex; align-items: center; gap: 6px; padding: 4px 0; }
#sort { border: 0; background: transparent; color: var(--link); font-size: 14px; max-width: 45%; }

.items { list-style: none; margin: 4px 16px; padding: 0; border-radius: 12px; overflow: hidden; background: var(--section); }
.items li { padding: 10px 12px; border-bottom: 1px solid var(--sep); cursor: pointer; }
.items li:last-child { border-bottom: 0; }
.items li:active { background: var(--bg2); }
.title { font-weight: 600; display: flex; gap: 6px; align-items: baseline; }
.title span:last-child { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.sum { color: var(--text); opacity: 0.85; font-size: 14px; margin-top: 2px;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.meta { display: flex; flex-wrap: wrap; gap: 4px 10px; color: var(--hint); font-size: 13px; margin-top: 4px; }
.badge { color: var(--accent); }
.badge.warn { color: var(--danger); }

.empty { text-align: center; color: var(--hint); padding: 32px 24px; }
.more { height: 1px; }

/* card */
#card-view { padding: 8px 16px 24px; }
.card { background: var(--section); border-radius: 12px; padding: 14px; }
.card h1 { font-size: 20px; margin: 4px 0 8px; line-height: 1.25; }
.card h1 a { color: inherit; text-decoration: none; }
.card .cat { color: var(--hint); font-size: 14px; }
.card p { margin: 8px 0; white-space: pre-wrap; }
.note { border-left: 3px solid var(--accent); padding-left: 10px; font-style: italic; }
.facts { list-style: none; padding: 0; margin: 10px 0; }
.facts li { padding: 4px 0; }
.facts b { font-weight: 600; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.tags button { border: 0; background: var(--bg2); color: var(--link); border-radius: 12px; padding: 3px 10px; font-size: 14px; }
details { margin-top: 10px; color: var(--hint); font-size: 14px; }
details p { color: var(--text); }

.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
.actions button {
  border: 0; border-radius: 12px; background: var(--section); padding: 10px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px;
}
.actions button .ic { font-size: 20px; }
.actions button.on { color: var(--accent); }
.list-actions { background: var(--section); border-radius: 12px; overflow: hidden; }
.list-actions button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 12px 14px; border-bottom: 1px solid var(--sep); }
.list-actions button:last-child { border-bottom: 0; }
.list-actions .danger { color: var(--danger); }

/* forms */
.form { padding: 12px 16px 24px; display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; color: var(--hint); font-size: 13px; flex: 1; }
.form input, .form textarea, .form select, .sheet input {
  border: 0; border-radius: 10px; padding: 10px 12px; background: var(--section); color: var(--text); font-size: 16px; width: 100%;
}
.form .row { display: flex; gap: 10px; }
.primary { border: 0; border-radius: 12px; padding: 12px; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.secondary { border: 0; border-radius: 12px; padding: 12px; background: var(--bg2); }

/* bottom sheet */
.sheet { position: fixed; inset: 0; z-index: 5; background: rgba(0, 0, 0, 0.4); display: flex; align-items: flex-end; }
.sheet-body {
  width: 100%; max-height: 85vh; overflow-y: auto; background: var(--bg2);
  border-radius: 16px 16px 0 0; padding: 16px 16px calc(var(--bottom) + 16px);
}
.sheet h2 { font-size: 15px; color: var(--hint); font-weight: 500; margin: 14px 0 4px; }
.sheet h2:first-child { margin-top: 0; }
.sheet .chips { flex-wrap: wrap; overflow: visible; }
.sheet .buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.sheet .presets { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.sheet .presets button { border: 0; border-radius: 12px; padding: 12px; background: var(--section); text-align: left; }
