:root {
  --bg: #0e1014;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9097a6;
  --accent: #8b6cff;
  --green: #3ecf8e;
  --amber: #f0b429;
  --blue: #4da3ff;
  --red: #ff5c6c;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }

/* --- buttons & inputs ---------------------------------------------------- */
.btn {
  border: 1px solid var(--border); background: var(--panel-2); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3b4252; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.danger.confirm { background: var(--red); color: #fff; border-color: var(--red); }
.btn.small { padding: 5px 10px; font-size: 13px; }
input[type=search], input[type=url], input[type=password], textarea {
  width: 100%; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* --- layout -------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: rgba(14, 16, 20, .92);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: .3px; }
.topbar form { margin: 0; }
.wrap { max-width: 1200px; margin: 0 auto; padding: 14px 16px 40px; }

.toolbar { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.add-form { display: flex; gap: 8px; }
@media (max-width: 700px) { .toolbar { grid-template-columns: 1fr; } }

.filters { display: flex; gap: 6px; margin: 14px 0; overflow-x: auto; scrollbar-width: none; }
.filters button {
  border: 1px solid var(--border); background: transparent; border-radius: 999px; padding: 6px 12px;
  cursor: pointer; white-space: nowrap; color: var(--muted);
}
.filters button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.filters span { opacity: .7; font-size: 12px; margin-left: 2px; }

/* --- grid ---------------------------------------------------------------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
@media (max-width: 420px) { .grid { grid-template-columns: 1fr 1fr; gap: 10px; } }

.groups { display: flex; flex-direction: column; gap: 28px; }
.group-title { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 8px; font-size: 17px; margin: 0 0 10px; }
.group-desc { color: var(--muted); font-size: 13px; font-weight: normal; }
.group-count {
  margin-left: auto; color: var(--muted); font-size: 12px; font-weight: 600; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 999px; padding: 1px 9px;
}
.cat-select {
  width: 100%; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 8px; font-size: 13px; cursor: pointer;
}
.type-picker { display: inline-flex; align-self: flex-start; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.type-picker button { background: transparent; border: 0; padding: 7px 14px; cursor: pointer; color: var(--muted); }
.type-picker button + button { border-left: 1px solid var(--border); }
.type-picker button.active { background: var(--accent); color: #fff; font-weight: 600; }

.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; display: flex; flex-direction: column; cursor: pointer; transition: border-color .15s;
}
.card:hover { border-color: #3b4252; }
.thumb { position: relative; aspect-ratio: 9 / 16; background: var(--panel-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .placeholder {
  position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 32px;
}
.thumb .badge { position: absolute; top: 8px; left: 8px; }
.card-body { padding: 10px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.author { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preview {
  color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  overflow: hidden; min-height: 3.9em;
}
.card .btn { margin-top: auto; }

.badge {
  display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px;
  padding: 3px 7px; border-radius: 6px; background: rgba(0, 0, 0, .65); backdrop-filter: blur(4px);
}
.badge.new { color: var(--green); }
.badge.used { color: var(--muted); }
.badge.pending { color: var(--amber); }
.badge.processing { color: var(--blue); animation: pulse 1.4s infinite; }
.badge.failed { color: var(--red); }
@keyframes pulse { 50% { opacity: .45; } }

.empty { color: var(--muted); text-align: center; padding: 50px 10px; }

/* --- detail modal -------------------------------------------------------- */
.modal { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .7); }
.modal-panel {
  position: relative; width: min(1000px, 96vw); max-height: 92vh; overflow-y: auto; background: var(--panel);
  border: 1px solid var(--border); border-radius: 16px; padding: 20px;
}
.modal-close {
  position: sticky; top: 0; float: right; z-index: 2; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 50%; width: 34px; height: 34px; cursor: pointer;
}
.detail { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 22px; }
.embed iframe { width: 100%; height: 640px; border: 0; border-radius: var(--radius); background: #fff; }
.detail-info { min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.detail-head h2 { margin: 0 0 4px; font-size: 20px; word-break: break-word; }
.meta { color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.section h3 {
  display: flex; justify-content: space-between; align-items: center; margin: 0 0 6px; font-size: 13px;
  text-transform: uppercase; letter-spacing: .5px; color: var(--muted);
}
.text-block {
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px; white-space: pre-wrap;
  word-break: break-word; max-height: 320px; overflow-y: auto;
}
.error-box { background: rgba(255, 92, 108, .1); border: 1px solid rgba(255, 92, 108, .4); border-radius: 8px; padding: 12px; }
.segments { margin-top: 8px; }
.segments summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.segments ol { list-style: none; padding: 0; margin: 8px 0 0; max-height: 280px; overflow-y: auto; }
.segments li { display: flex; gap: 10px; padding: 3px 0; font-size: 14px; }
.segments time { color: var(--accent); font-variant-numeric: tabular-nums; flex: none; }
textarea { min-height: 90px; resize: vertical; }
.saved-hint { font-size: 12px; color: var(--muted); font-weight: normal; text-transform: none; letter-spacing: 0; }

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { appearance: none; width: 38px; height: 22px; border-radius: 999px; background: var(--border); position: relative; cursor: pointer; margin: 0; }
.switch input::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff;
  transition: transform .15s;
}
.switch input:checked { background: var(--green); }
.switch input:checked::after { transform: translateX(16px); }

@media (max-width: 760px) {
  .modal { place-items: stretch; }
  .modal-panel { width: 100vw; max-height: none; height: 100%; border-radius: 0; border: 0;
    padding: calc(14px + env(safe-area-inset-top)) 14px calc(30px + env(safe-area-inset-bottom)); }
  .detail { grid-template-columns: 1fr; }
  .embed iframe { height: 560px; }
}

/* --- toast & login ------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 999px; font-weight: 600; z-index: 50;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .4); max-width: 90vw;
}
.toast.error { background: var(--red); color: #fff; }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-box {
  width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 16px; padding: 28px;
}
.login-box h1 { margin: 0 0 6px; font-size: 22px; text-align: center; }
.login-error { color: var(--red); margin: 0; text-align: center; }
