:root {
  --bg: #191919;
  --bg-soft: #202020;
  --bg-hover: #2a2a2a;
  --bg-active: #303030;
  --text: #d4d4d2;
  --text-dim: #8b8b88;
  --line: #303030;
  --accent: #8b1d2f;
  --accent-hover: #a52439;
  --accent-soft: #3b121b;
  --danger: #d33a3a;
  --field: #252525;
  --shadow: 0 6px 28px rgba(0, 0, 0, .55);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 16px;
  font-weight: 700;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  font-weight: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

.hidden { display: none !important; }

/* shared fade on hover — every button in the app uses it */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.ghost, .foot-btn, .mini, .seg button, .crumb, .row-act, .row, .twist,
.empty-box button, .login-card button, .head-del, .search-hit {
  transition: background-color .2s ease, color .2s ease, opacity .2s ease,
              border-color .2s ease, transform .12s ease, filter .2s ease;
}

.ghost:hover, .foot-btn:hover, .mini:hover, .seg button:hover, .crumb:hover,
.row-act:hover, .search-hit:hover, .head-del:hover {
  animation: fadeIn .25s ease both;
}

button:active { transform: translateY(1px); }

/* ------------------------------------------------------------------ login */

.login {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: 20px;
}

.login-card {
  width: min(340px, 100%);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 28px 24px 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}

.login-logo { font-size: 26px; font-weight: 800; letter-spacing: -.02em; }
.login-sub { color: var(--text-dim); font-size: 13px; margin-top: -6px; }

.login-card input {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 16px;
  outline: none;
  background: var(--field);
  color: var(--text);
}
.login-card input:focus { border-color: var(--accent); }

.login-card button {
  background: var(--accent);
  color: #fff;
  padding: 12px;
  border-radius: 6px;
  font-weight: 700;
}
.login-card button:hover { background: var(--accent-hover); }

.login-err { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ------------------------------------------------------------------- app */

.app {
  display: grid;
  grid-template-columns: 248px 1fr;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
}

body.side-collapsed .app { grid-template-columns: 0 1fr; }
body.side-collapsed .sidebar { transform: translateX(-248px); }

/* --------------------------------------------------------------- sidebar */

.sidebar {
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-width: 248px;
  transition: transform .2s ease;
  position: relative;
  z-index: 5;
  height: 100%;
}

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 8px;
  font-weight: 800;
  letter-spacing: -.01em;
}

.ghost {
  color: var(--text-dim);
  border-radius: 5px;
  padding: 4px 9px;
  font-size: 13px;
  min-height: 28px;
}
.ghost:hover { background: var(--bg-hover); color: var(--text); }

.side-search { padding: 6px 10px 4px; position: relative; }

.side-search input {
  width: 100%;
  padding: 9px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: var(--field);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.side-search input:focus { background: var(--field); border-color: var(--accent); }
.side-search input::placeholder { color: var(--text-dim); }
.side-search input::-webkit-search-cancel-button { display: none; }

.search-results {
  position: absolute;
  left: 10px;
  right: 10px;
  top: 48px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: 320px;
  overflow: auto;
  z-index: 40;
  padding: 4px;
}

.search-hit {
  display: flex;
  gap: 8px;
  align-items: center;
  width: 100%;
  text-align: left;
  padding: 9px 8px;
  border-radius: 5px;
  font-size: 14px;
}
.search-hit:hover { background: var(--bg-hover); }

.tree {
  flex: 1;
  overflow-y: auto;
  padding: 6px 6px 10px;
  -webkit-overflow-scrolling: touch;
}

.tree-empty { color: var(--text-dim); font-size: 13px; padding: 8px 10px; }

.row {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 30px;
  padding: 2px 4px 2px 0;
  border-radius: 5px;
  cursor: pointer;
  user-select: none;
  font-size: 14.5px;
}
.row:hover { background: var(--bg-hover); }
.row.active { background: var(--bg-active); }

.twist {
  width: 18px;
  min-width: 18px;
  height: 18px;
  color: var(--text-dim);
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  border-radius: 3px;
  transition: transform .12s ease;
  padding: 0;
}
.twist.open { transform: rotate(90deg); }
.twist:not(.notwist):hover { background: var(--bg-active); }

.row-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-act {
  opacity: 0;
  font-size: 15px;
  color: var(--text-dim);
  width: 26px;
  height: 24px;
  border-radius: 4px;
  flex: none;
}
.row:hover .row-act { opacity: 1; }
.row-act:hover { background: var(--bg-active); color: var(--text); }

.side-foot {
  border-top: 1px solid var(--line);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.foot-btn {
  text-align: left;
  padding: 9px 8px;
  border-radius: 5px;
  font-size: 14px;
  color: var(--text);
}
.foot-btn:hover { background: var(--bg-hover); }
.foot-btn.dim { color: var(--text-dim); }

.pill {
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  font-size: 11px;
  padding: 1px 7px;
  margin-left: 6px;
}

/* --------------------------------------------------------- mobile chrome */

.scrim { display: none; }

.topbar { display: none; }

/* ------------------------------------------------------------------ main */

.main {
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.page {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.crumbs {
  padding: 12px 36px 0;
  font-size: 13px;
  color: var(--text-dim);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.crumb { color: var(--text-dim); font-size: 13px; padding: 3px 5px; border-radius: 4px; }
.crumb:hover { background: var(--bg-hover); color: var(--text); }
.sep { opacity: .55; }

.page-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 36px 10px;
}

.title {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -.02em;
  padding: 4px 0;
  background: transparent;
  color: var(--text);
}
.title::placeholder { color: #5a5a57; }

.head-right { display: flex; align-items: center; gap: 10px; flex: none; }

.save-state {
  font-size: 12px;
  color: var(--text-dim);
  min-width: 78px;
  text-align: right;
}

.head-del {
  font-size: 13px;
  padding: 6px 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  background: var(--bg-soft);
}
.head-del:hover { color: var(--danger); border-color: var(--danger); background: transparent; }

.seg {
  display: flex;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 2px;
}
.seg button {
  font-size: 13px;
  padding: 5px 11px;
  border-radius: 5px;
  color: var(--text-dim);
}
.seg button.on { background: var(--bg-active); color: var(--text); }

/* ---------------------------------------------------------------- editor */

.editor-wrap {
  flex: 1;
  display: grid;
  min-height: 0;
  border-top: 1px solid var(--line);
}
.editor-wrap.mode-edit { grid-template-columns: 1fr; }
.editor-wrap.mode-edit #preview { display: none; }
.editor-wrap.mode-preview { grid-template-columns: 1fr; }
.editor-wrap.mode-preview #editor { display: none; }
.editor-wrap.mode-split { grid-template-columns: 1fr 1fr; }
.editor-wrap.mode-split #editor { border-right: 1px solid var(--line); }

#editor {
  border: none;
  outline: none;
  resize: none;
  padding: 26px 36px 80px;
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  caret-color: var(--accent-hover);
}
#editor::placeholder { color: #5a5a57; }

.preview {
  overflow-y: auto;
  padding: 26px 36px 120px;
  max-width: 920px;
  line-height: 1.75;
  -webkit-overflow-scrolling: touch;
}
.preview.plain { white-space: pre-wrap; font-family: var(--mono); font-size: 14px; }

.preview h1, .preview h2, .preview h3,
.preview h4, .preview h5, .preview h6 {
  margin: 1.3em 0 .45em;
  line-height: 1.3;
  font-weight: 800;
  letter-spacing: -.015em;
  color: #efefed;
}
.preview h1 { font-size: 1.9em; margin-top: .4em; }
.preview h2 { font-size: 1.45em; border-bottom: 1px solid var(--line); padding-bottom: .25em; }
.preview h3 { font-size: 1.2em; }
.preview p { margin: .55em 0; }
.preview ul, .preview ol { padding-left: 1.5em; margin: .5em 0; }
.preview li { margin: .2em 0; }
.preview li::marker { color: var(--text-dim); }

.preview blockquote {
  margin: .8em 0;
  padding: .1em 0 .1em 1em;
  border-left: 3px solid var(--accent);
  color: #a5a5a1;
}

.preview code {
  font-family: var(--mono);
  font-size: .88em;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: .1em .35em;
  color: #e08b96;
}

.preview pre {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
}
.preview pre code { background: none; border: none; padding: 0; color: var(--text); }

.preview a { color: var(--accent-hover); text-decoration: underline; text-underline-offset: 2px; }
.preview hr { border: none; border-top: 1px solid var(--line); margin: 1.6em 0; }
.preview img { max-width: 100%; border-radius: 6px; }
.preview table { border-collapse: collapse; margin: .8em 0; width: 100%; }
.preview th, .preview td { border: 1px solid var(--line); padding: 6px 10px; font-size: .95em; }
.preview th { background: var(--bg-soft); }
.preview del { color: var(--text-dim); }

input[type="checkbox"] { accent-color: var(--accent); margin-right: .4em; }

::selection { background: var(--accent-soft); }

/* ---------------------------------------------------------------- panels */

.empty { display: grid; place-items: center; height: 100%; padding: 20px; }
.empty-box { text-align: center; color: var(--text-dim); }
.empty-box p { margin: 0 0 16px; }
.empty-box button {
  background: var(--accent);
  color: #fff;
  padding: 10px 18px;
  border-radius: 7px;
  font-weight: 700;
}
.empty-box button:hover { background: var(--accent-hover); }

.panel {
  position: absolute;
  inset: 0;
  background: var(--bg);
  z-index: 30;
  display: flex;
  flex-direction: column;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 36px 12px;
  border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 22px; font-weight: 800; }
.trash-list { overflow-y: auto; padding: 12px 36px; -webkit-overflow-scrolling: touch; }
.trash-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px;
  border-radius: 7px;
}
.trash-row:hover { background: var(--bg-soft); }
.trash-left { display: flex; align-items: center; gap: 10px; font-size: 15px; min-width: 0; }
.trash-meta { color: var(--text-dim); font-size: 12.5px; white-space: nowrap; }
.trash-right { display: flex; gap: 8px; flex: none; }

.mini {
  font-size: 13px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-soft);
}
.mini:hover { background: var(--bg-hover); }
.mini.danger { color: var(--danger); }
.mini.danger:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ----------------------------------------------------------------- toast */

.toast {
  position: absolute;
  bottom: calc(22px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #efefed;
  color: #191919;
  font-size: 13.5px;
  font-weight: 700;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 80;
  max-width: calc(100% - 32px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ------------------------------------------------------------- scrollbars */

* { scrollbar-width: thin; scrollbar-color: #3d3d3d transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: #3d3d3d; border-radius: 999px; border: 2px solid var(--bg); }
*::-webkit-scrollbar-thumb:hover { background: var(--accent); }
*::-webkit-scrollbar-track { background: transparent; }

/* ------------------------------------------------------------- responsive */

@media (hover: none) {
  .row-act { opacity: 1; }
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }

  .topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px calc(10px + env(safe-area-inset-top));
    padding-top: calc(10px + env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
    background: var(--bg-soft);
    flex: none;
  }
  .topbar-title { font-weight: 800; font-size: 15px; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(84vw, 310px);
    min-width: 0;
    transform: translateX(-102%);
    transition: transform .22s ease;
    z-index: 70;
    box-shadow: var(--shadow);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.nav-open .sidebar { transform: none; }
  body.side-collapsed .sidebar { transform: translateX(-102%); }

  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    z-index: 60;
  }
  body.nav-open .scrim { opacity: 1; pointer-events: auto; }

  .row { min-height: 40px; font-size: 15.5px; }
  .twist { width: 24px; height: 24px; line-height: 24px; }
  .row-act { width: 34px; height: 32px; font-size: 18px; }
  .foot-btn, .ghost { padding: 11px 8px; font-size: 15px; }
  .side-search input { padding: 11px 10px; }

  .crumbs {
    padding: 10px 16px 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    white-space: nowrap;
    scrollbar-width: none;
  }
  .crumbs::-webkit-scrollbar { display: none; }

  .page-head {
    flex-wrap: wrap;
    padding: 10px 16px 6px;
    gap: 6px;
  }
  .title { font-size: 24px; flex-basis: 100%; order: -1; }
  .head-right { width: 100%; justify-content: space-between; }
  .save-state { min-width: 0; }
  .seg { flex: 1; justify-content: center; }
  .seg button { flex: 1; padding: 9px 6px; font-size: 13px; }
  .head-del { padding: 9px 12px; }

  .editor-wrap.mode-split { grid-template-columns: 1fr; }
  .editor-wrap.mode-split #preview { display: none; }

  #editor, .preview { padding: 18px 16px calc(90px + env(safe-area-inset-bottom)); }
  #editor { font-size: 16px; }  /* 16px stops iOS Safari from zooming on focus */
  .preview { font-size: 15.5px; }
  .preview h1 { font-size: 1.6em; }
  .preview h2 { font-size: 1.3em; }

  .panel-head { padding: 14px 16px 10px; }
  .trash-list { padding: 8px 10px; }
  .trash-row { flex-direction: column; align-items: stretch; gap: 10px; }
  .trash-right { justify-content: flex-end; }
  .mini { padding: 10px 12px; }

  .toast { white-space: normal; text-align: center; width: max-content; }
}

@media (min-width: 901px) {
  body.nav-open .scrim { display: none; }
}

@media (max-width: 420px) {
  .head-right { flex-wrap: wrap; gap: 8px; }
  .seg { flex-basis: 100%; order: 2; }
}

/* ------------------------------------------------------------ auth extras */

.tabs {
  display: flex;
  gap: 4px;
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 2px;
}
.tab {
  flex: 1;
  font-size: 13.5px;
  padding: 8px 10px;
  border-radius: 5px;
  color: var(--text-dim);
  transition: background-color .2s ease, color .2s ease;
}
.tab.on { background: var(--bg-active); color: var(--text); }
.tab:not(.on):hover { color: var(--text); }

.login-note { color: var(--text-dim); font-size: 12.5px; }

#licblock .login-card button.ghost-link,
.login-card button.ghost-link {
  background: none;
  color: var(--text-dim);
  font-size: 13.5px;
  padding: 7px;
}
.login-card button.ghost-link:hover { background: none; color: var(--text); text-decoration: underline; }

.who-line {
  font-size: 12.5px;
  color: var(--text-dim);
  padding: 2px 8px 8px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------------------------------------------------------- license panel */

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 36px 44px;
  -webkit-overflow-scrolling: touch;
}

.lic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}
.lic-cell {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.lic-cell span {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .07em;
}
.lic-cell b { font-size: 15px; overflow: hidden; text-overflow: ellipsis; }
.lic-cell b.ok, .ok { color: #8fbf97; }
.lic-cell b.bad, .bad { color: var(--danger); }

.lic-sec {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-dim);
  margin: 22px 0 8px;
}

.lic-device {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-soft);
  margin-bottom: 8px;
}
.lic-device.cur { border-color: var(--accent); }
.lic-device.off { border-style: dashed; }
.lic-dev-info { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.lic-dev-info b { font-size: 14.5px; }
.lic-dev-info code {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  font-size: 11.5px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-dim);
  flex: none;
}
.badge.ok { color: #8fbf97; border-color: #31543c; }

.lic-msg { font-size: 13.5px; padding: 4px 0; }
.dim, .lic-dev-info .dim { color: var(--text-dim); font-size: 12.5px; }
.lic-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.assert { font-family: var(--mono); font-size: 12.5px; font-weight: 700; }
.assert.good { color: #8fbf97; }
.assert.bad { color: var(--danger); }

/* ------------------------------------------------------------------ admin */

.admin-head { gap: 14px; }

.mint-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.mint-row select, .mint-row input {
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  padding: 8px 10px;
  outline: none;
  transition: border-color .2s ease;
}
.mint-row select:focus, .mint-row input:focus { border-color: var(--accent); }
.mint-row input[type="number"] { width: 86px; }
.mint-row #mint-note { flex: 1; min-width: 150px; }

.mini.accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.mini.accent:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.keys-box {
  background: var(--bg-soft);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 14px;
}
.keys-title { font-size: 12.5px; color: var(--text-dim); margin-bottom: 8px; }
.key-line { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 5px 0; }
.key-line code {
  font-family: var(--mono);
  font-size: 13.5px;
  color: #e08b96;
  word-break: break-all;
}

.tbl { display: flex; flex-direction: column; gap: 4px; overflow-x: auto; }
.tbl-head, .tbl-row {
  display: grid;
  grid-template-columns: 1.3fr .7fr .8fr 1fr .8fr 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 9px 12px;
  font-size: 13.5px;
  min-width: 620px;
}
.tbl-head {
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
}
.tbl-row {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 7px;
  transition: background-color .2s ease;
}
.tbl-row:hover { background: var(--bg-hover); }
.tbl-row code { font-family: var(--mono); font-size: 12.5px; }
.tbl-head.audit, .tbl-row.audit { grid-template-columns: 1.2fr .8fr .9fr 1.1fr 2fr; }

@media (max-width: 900px) {
  .panel-body { padding: 12px 16px calc(36px + env(safe-area-inset-bottom)); }
  .admin-head { flex-wrap: wrap; }
  .admin-head h2 { flex-basis: 100%; }
  .lic-device { flex-direction: column; align-items: stretch; }
  .lic-device .mini, .lic-device .badge { align-self: flex-start; }
  .mint-row input[type="number"] { width: 74px; }
  .lic-grid { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
}
