:root {
  --bg: #0f172a;
  --panel: #111827;
  --panel2: #172033;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --border: #263247;
  --brand: #38bdf8;
  --brand2: #0ea5e9;
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --input: #0b1220;
  --tap: 44px;
}
* { box-sizing: border-box; }
html { min-width: 0; -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-width: 0;
  overflow-x: hidden;
}
a { color: inherit; }
body.drawer-open { overflow: hidden; }
.mobile-only { display: none !important; }

.topbar {
  min-height: 64px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 23, 42, .97);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-block { min-width: 0; }
.brand-title { font-weight: 800; letter-spacing: .01em; }
.root-label { display: block; max-width: min(68vw, 820px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.topbar nav { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

.layout { display: grid; grid-template-columns: 360px minmax(0, 1fr); min-height: calc(100vh - 64px); }
.sidebar {
  border-right: 1px solid var(--border);
  padding: 14px;
  overflow: auto;
  max-height: calc(100vh - 64px);
  scrollbar-gutter: stable;
}
.main {
  padding: 14px;
  display: grid;
  grid-template-rows: minmax(360px, 58vh) auto;
  gap: 14px;
  min-width: 0;
}
.panel, .auth-card, .modal-card {
  background: linear-gradient(180deg, var(--panel), #0d1424);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
.panel { padding: 14px; margin-bottom: 14px; min-width: 0; }
h1, h2 { margin: 0 0 10px; line-height: 1.15; }
h1 { font-size: 24px; } h2 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; min-width: 0; }
.wrap { flex-wrap: wrap; }
.right { justify-content: flex-end; }
.stack { display: grid; gap: 12px; }
label { display: grid; gap: 6px; color: var(--text); font-size: 14px; }
input, textarea {
  width: 100%;
  background: var(--input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  outline: none;
  min-height: var(--tap);
}
input:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(56,189,248,.15); }
button, .button {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  min-height: var(--tap);
  background: var(--brand2);
  color: white;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:hover, .button:hover { filter: brightness(1.08); }
button:disabled { opacity: .45; cursor: not-allowed; }
.secondary { background: #233149; color: #dbeafe; }
.danger { background: var(--danger); }
.smallbtn { padding: 8px 10px; font-size: 12px; min-height: 38px; }
.iconbtn { width: var(--tap); padding: 0; font-size: 20px; }
.auth-body { min-height: 100vh; display: grid; place-items: center; padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); }
.auth-card { width: min(460px, 100%); padding: 26px; }
.auth-card.wide { width: min(760px, 100%); }
.alert { padding: 12px; border-radius: 12px; margin: 10px 0; border: 1px solid var(--border); }
.alert.error { background: rgba(239,68,68,.1); border-color: rgba(239,68,68,.45); }
.alert.success { background: rgba(34,197,94,.1); border-color: rgba(34,197,94,.45); }
.alert.warn { background: rgba(245,158,11,.1); border-color: rgba(245,158,11,.45); }
.path {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  background: var(--input);
  border: 1px solid var(--border);
  padding: 8px;
  border-radius: 10px;
  margin: 8px 0;
  overflow-wrap: anywhere;
}
.file-list, .results { display: grid; gap: 6px; max-height: 340px; overflow: auto; -webkit-overflow-scrolling: touch; }
.file-item, .result-item, .backup-item {
  display: grid;
  gap: 3px;
  padding: 10px;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255,255,255,.03);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.file-item:hover, .result-item:hover, .backup-item:hover, .file-item:focus-visible, .result-item:focus-visible, .backup-item:focus-visible { border-color: var(--brand); outline: none; }
.file-item .name { font-weight: 700; overflow-wrap: anywhere; }
.file-item.dir .name::before { content: '📁 '; }
.file-item.file .name::before { content: '📄 '; }
.editor-panel { min-height: 0; display: grid; grid-template-rows: auto 1fr auto; gap: 10px; }
.editor-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; min-width: 0; }
.editor-head > div:first-child { min-width: 0; }
#editorTitle { overflow-wrap: anywhere; }
#editor {
  height: 100%;
  min-height: 320px;
  resize: none;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 13px;
  line-height: 1.45;
  white-space: pre;
  tab-size: 2;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.check { display: flex; align-items: center; gap: 8px; grid-template-columns: auto 1fr; color: var(--muted); }
.check input { width: auto; min-height: auto; }
.command { min-height: 86px; resize: vertical; }
.diff { max-height: 420px; overflow: auto; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; border-radius: 12px; border: 1px solid var(--border); margin-top: 12px; -webkit-overflow-scrolling: touch; }
.diff-line { padding: 3px 8px; white-space: pre-wrap; overflow-wrap: anywhere; border-bottom: 1px solid rgba(255,255,255,.03); }
.diff-line.same { color: #94a3b8; }
.diff-line.add { background: rgba(34,197,94,.16); color: #d1fae5; }
.diff-line.del { background: rgba(239,68,68,.16); color: #fee2e2; }
.ai-summary { margin-top: 10px; color: #dbeafe; overflow-wrap: anywhere; }
.toast {
  position: fixed;
  bottom: calc(18px + env(safe-area-inset-bottom));
  right: 18px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #020617;
  border: 1px solid var(--border);
  box-shadow: 0 14px 50px rgba(0,0,0,.4);
  opacity: 0;
  transform: translateY(10px);
  transition: .2s;
  pointer-events: none;
  max-width: min(420px, calc(100vw - 24px));
  z-index: 80;
}
.toast.show { opacity: 1; transform: translateY(0); }
dialog { border: 0; padding: 0; background: transparent; color: var(--text); width: min(720px, 95vw); }
dialog::backdrop { background: rgba(0,0,0,.6); }
.modal-card { padding: 18px; display: grid; gap: 12px; }
code { background: rgba(255,255,255,.08); border: 1px solid var(--border); border-radius: 6px; padding: 2px 5px; overflow-wrap: anywhere; }
.drawer-backdrop { display: none; }

@media (max-width: 980px) {
  .desktop-only { display: none !important; }
  .mobile-only { display: inline-flex !important; }
  .topbar {
    min-height: 58px;
    padding: 9px 10px calc(9px + env(safe-area-inset-top) / 3);
    gap: 8px;
  }
  .topbar-left { gap: 8px; flex: 1; }
  .brand-title { display: block; font-size: 15px; }
  .root-label { max-width: calc(100vw - 165px); font-size: 11px; }
  .topbar nav { gap: 6px; }
  .topbar nav .button, .topbar nav button { padding-left: 10px; padding-right: 10px; }

  .layout { display: block; min-height: calc(100vh - 58px); }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: min(92vw, 390px);
    max-height: none;
    padding: calc(14px + env(safe-area-inset-top)) 14px calc(18px + env(safe-area-inset-bottom));
    border-right: 1px solid var(--border);
    border-bottom: 0;
    background: var(--bg);
    z-index: 60;
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 20px 0 70px rgba(0,0,0,.55);
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.56);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    z-index: 55;
  }
  body.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
  .sidebar-mobile-head {
    display: flex !important;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
  }
  .main {
    padding: 10px;
    grid-template-rows: auto auto;
    gap: 10px;
  }
  .panel { border-radius: 14px; padding: 12px; margin-bottom: 10px; }
  .editor-panel { min-height: calc(100dvh - 122px); grid-template-rows: auto minmax(360px, 1fr) auto; }
  .editor-head { display: grid; gap: 10px; }
  .editor-head .row { display: grid; grid-template-columns: 1fr 1fr; width: 100%; }
  .editor-head .row #saveBtn { grid-column: 1 / -1; }
  #editor {
    min-height: 52dvh;
    font-size: 14px;
    line-height: 1.5;
    padding: 12px;
    white-space: pre;
  }
  .ai-panel .row { display: grid; grid-template-columns: 1fr; }
  .file-list, .results { max-height: min(48dvh, 390px); }
  .row:not(.right):not(.wrap) { align-items: stretch; }
  .row:not(.right):not(.wrap) input { min-width: 0; }
  .diff { max-height: 46dvh; font-size: 12px; }
  .toast { left: 10px; right: 10px; bottom: calc(10px + env(safe-area-inset-bottom)); width: auto; }
  .auth-card { padding: 20px; border-radius: 16px; }
  h1 { font-size: 22px; }
}

@media (max-width: 560px) {
  :root { --tap: 46px; }
  .small { font-size: 11px; }
  .topbar nav .label-text { display: none; }
  .topbar nav .button, .topbar nav button { min-width: var(--tap); padding-left: 9px; padding-right: 9px; }
  .brand-title { font-size: 14px; }
  .root-label { max-width: calc(100vw - 148px); }
  .main { padding: 8px; }
  .panel { padding: 10px; }
  .editor-head .row { grid-template-columns: 1fr; }
  .editor-head .row #saveBtn { grid-column: auto; }
  #editor { min-height: 58dvh; font-size: 14px; }
  .command { min-height: 104px; }
  .modal-card { padding: 14px; }
  dialog { width: 96vw; }
  .search-row { display: grid !important; grid-template-columns: 1fr; }
  .auth-body { align-items: start; }
  .auth-card { margin-top: 14px; }
}

@media (pointer: coarse) {
  .file-item, .result-item, .backup-item { padding: 12px; }
  input, textarea { font-size: 16px; }
  button, .button { font-size: 14px; }
}
