:root {
  --bg: #1e1e1e;
  --panel: #252526;
  --panel2: #2d2d30;
  --border: #333;
  --accent: #007acc;
  --accent2: #0e639c;
  --text: #d4d4d4;
  --text-dim: #858585;
  --err: #f14c4c;
  --warn: #ddb100;
  --ok: #4ec9b0;
  --mono: 'JetBrains Mono', monospace;
  --fa: 'Vazirmatn', sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; padding: 0; max-width: 100%; overflow-x: hidden; }
body {
  font-family: var(--fa);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
#root { height: 100vh; height: 100dvh; width: 100%; }
button { font-family: var(--fa); }

.ide { display: flex; flex-direction: column; height: 100vh; height: 100dvh; width: 100%; }

/* ===== top bar ===== */
.topbar {
  display: flex; align-items: center; gap: 12px;
  background: linear-gradient(90deg, #1a1a1a, #252526);
  border-bottom: 1px solid var(--border);
  padding: 6px 12px; flex-wrap: wrap; flex-shrink: 0;
  padding-top: calc(6px + var(--safe-t));
}
.brand { display: flex; align-items: baseline; gap: 8px; flex-shrink: 0; }
.brand .logo { font-size: 20px; }
.brand > span:nth-child(2) { font-weight: 800; font-size: 18px; color: #fff; }
.brand .sub { font-size: 11px; color: var(--text-dim); font-weight: 300; }
.toolbar { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.divider { width: 1px; height: 22px; background: var(--border); margin: 0 4px; display: inline-block; }
.tb {
  position: relative;
  display: inline-flex; align-items: center; gap: 5px;
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  color: var(--text); padding: 5px 9px; cursor: pointer; font-size: 13px;
  transition: all .15s;
}
.tb:hover { background: var(--panel2); border-color: var(--border); }
.tb:active { transform: scale(.94); }
.tb-accent { background: var(--accent); color: #fff; }
.tb-accent:hover { background: var(--accent2); }
.tb-icon { font-size: 14px; }
.tb-dirty { color: var(--err); font-size: 8px; position: absolute; top: 2px; left: 2px; }
.save-status {
  margin-inline-start: auto; font-size: 12px; color: var(--ok);
  padding: 4px 10px; border-radius: 20px; background: rgba(78,201,176,.1);
}
.save-status.dirty { color: var(--warn); background: rgba(221,177,0,.12); }

/* mobile top area */
.mtop { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.mtop-file {
  flex: 1; min-width: 0; font-size: 13px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: ltr; text-align: left;
}
.mtop-dot { color: var(--warn); }
.mtop-actions { display: flex; gap: 4px; flex-shrink: 0; }
.big-touch { min-width: 44px; min-height: 44px; justify-content: center; font-size: 18px; }

/* ===== body layout (desktop) ===== */
.body { flex: 1 1 auto; display: flex; min-height: 0; overflow: hidden; }

.explorer {
  width: 230px; background: var(--panel); border-inline-start: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; min-height: 0;
}
.panel-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; font-size: 12px; color: var(--text-dim);
  letter-spacing: .3px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.ph-actions { display: flex; gap: 4px; }
.mini {
  background: transparent; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 13px; padding: 4px 7px; border-radius: 4px;
  min-width: 32px; min-height: 32px;
}
.mini:hover { background: var(--panel2); color: #fff; }
.mini:active { background: var(--accent); color: #fff; }
.tree { flex: 1; overflow-y: auto; padding: 6px; font-size: 13px; min-height: 0; -webkit-overflow-scrolling: touch; }
.tree-root { padding: 4px 6px; color: var(--text-dim); font-weight: 600; }
.tree-folder { padding: 4px 6px 4px 20px; color: var(--warn); }
.tree-file {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px; border-radius: 4px; cursor: pointer;
}
.tree-file:hover { background: var(--panel2); }
.tree-file.active { background: rgba(0,122,204,.18); color: #fff; }
.tf-dot { color: var(--warn); font-size: 9px; }
.tf-actions { display: none; }
.tree-file:hover .tf-actions { display: inline-flex; gap: 2px; }
.explorer-foot { border-top: 1px solid var(--border); padding: 8px; flex-shrink: 0; }
.ai-teaser { font-size: 11px; color: var(--text-dim); text-align: center; cursor: pointer; }

.center { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }

.tabbar {
  display: flex; align-items: stretch; background: var(--panel);
  border-bottom: 1px solid var(--border); overflow-x: auto; flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}
.tabbar::-webkit-scrollbar { height: 3px; }
.ex-toggle { background: transparent; border: none; color: var(--text-dim); cursor: pointer; padding: 0 12px; font-size: 16px; flex-shrink: 0; }
.ex-toggle:hover { color: #fff; }
.tab {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px;
  border-inline-end: 1px solid var(--border); cursor: pointer;
  font-size: 12px; color: var(--text-dim); white-space: nowrap;
  border-top: 2px solid transparent; flex-shrink: 0;
}
.tab.active { background: var(--bg); color: #fff; border-top-color: var(--accent); }
.tab-dot { color: var(--warn); font-size: 9px; }
.tab-close { color: var(--text-dim); font-size: 16px; padding: 0 4px; }
.tab-close:hover { color: var(--err); }

.searchbar {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px;
  background: var(--panel2); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.search-input {
  flex: 1; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); padding: 8px; border-radius: 5px; font-family: var(--mono); font-size: 16px;
}
.search-count { font-size: 11px; color: var(--text-dim); }

/* ===== editor ===== */
.editor-shell { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.editor-wrap { flex: 1 1 auto; display: flex; min-height: 0; background: var(--bg); position: relative; overflow: hidden; }
.gutter {
  width: 48px; flex-shrink: 0; background: var(--bg); overflow: hidden;
  text-align: center; color: var(--text-dim); font-family: var(--mono);
  font-size: 13px; line-height: 21px; padding-top: 8px; direction: ltr;
  border-inline-end: 1px solid var(--border);
}
.gutter-line { height: 21px; padding-inline-end: 8px; text-align: right; }
.gutter-err { color: var(--err); font-weight: bold; cursor: help; }
.code-area { flex: 1; position: relative; overflow: hidden; }
.code-pre, .code-ta {
  margin: 0; padding: 8px 12px; font-family: var(--mono); font-size: 13px;
  line-height: 21px; white-space: pre; direction: ltr; text-align: left;
  position: absolute; inset: 0; overflow: auto; tab-size: 2;
}
.code-pre { pointer-events: none; z-index: 1; color: var(--text); }
.code-pre code { font-family: var(--mono); }
.code-ta {
  z-index: 2; background: transparent; color: transparent; caret-color: #fff;
  border: none; outline: none; resize: none; -webkit-overflow-scrolling: touch;
}
.code-ta::selection { background: rgba(0,122,204,.35); }

/* tokens */
.tk-kw { color: #c586c0; }
.tk-str { color: #ce9178; }
.tk-com { color: #6a9955; font-style: italic; }
.tk-num { color: #b5cea8; }
.tk-tag { color: #569cd6; }
.tk-attr { color: #9cdcfe; }
.tk-sel { color: #d7ba7d; }
.tk-prop { color: #9cdcfe; }
.tk-val { color: #ce9178; }
.tk-find { background: #613214; color: #fff; border-radius: 2px; }

/* autocomplete suggestion strip */
.suggest-strip {
  display: flex; gap: 6px; overflow-x: auto; flex-shrink: 0;
  background: var(--panel2); border-top: 1px solid var(--border);
  padding: 6px 8px; -webkit-overflow-scrolling: touch;
}
.suggest-strip::-webkit-scrollbar { height: 0; }
.suggest-chip {
  flex-shrink: 0; background: var(--bg); border: 1px solid var(--border);
  color: #9cdcfe; font-family: var(--mono); font-size: 13px;
  padding: 7px 12px; border-radius: 6px; cursor: pointer; min-height: 36px;
}
.suggest-chip:active { background: var(--accent); color: #fff; }

.no-file { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-dim); padding: 24px; text-align: center; }

/* ===== bottom panel ===== */
.bottom {
  height: 220px; flex-shrink: 0; background: var(--panel);
  border-top: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0;
}
.bottom-tabs { display: flex; align-items: center; gap: 4px; padding: 4px 8px; border-bottom: 1px solid var(--border); flex-shrink: 0; flex-wrap: wrap; }
.bt {
  background: transparent; border: none; color: var(--text-dim);
  cursor: pointer; padding: 7px 12px; font-size: 12px; border-radius: 5px;
  border-bottom: 2px solid transparent;
}
.bt.active { color: #fff; border-bottom-color: var(--accent); }
.bt-spacer { flex: 1; }
.run-status { font-size: 11px; padding: 3px 8px; border-radius: 12px; }
.rs-ok { color: var(--ok); }
.rs-error { color: var(--err); }
.rs-idle { color: var(--text-dim); }

.console { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.console-filters { display: flex; gap: 4px; padding: 6px 8px; flex-wrap: wrap; flex-shrink: 0; overflow-x: auto; }
.flt { background: var(--panel2); border: 1px solid var(--border); color: var(--text-dim); cursor: pointer; font-size: 12px; padding: 6px 12px; border-radius: 14px; flex-shrink: 0; min-height: 32px; }
.flt.active { color: #fff; }
.flt-error.active { background: rgba(241,76,76,.25); border-color: var(--err); }
.flt-warn.active { background: rgba(221,177,0,.2); border-color: var(--warn); }
.flt-log.active, .flt-all.active { background: var(--accent); border-color: var(--accent); }
.console-body { flex: 1; overflow-y: auto; padding: 4px 8px; font-family: var(--mono); font-size: 12px; min-height: 0; -webkit-overflow-scrolling: touch; }
.console-empty { color: var(--text-dim); padding: 16px; text-align: center; font-family: var(--fa); }
.log {
  display: flex; align-items: baseline; gap: 8px; padding: 6px;
  border-bottom: 1px solid rgba(255,255,255,.04); cursor: pointer;
}
.log:hover, .log:active { background: var(--panel2); }
.log-badge { font-size: 10px; padding: 1px 6px; border-radius: 4px; flex-shrink: 0; font-family: var(--fa); }
.log-log .log-badge { background: #3a3a3a; color: #ccc; }
.log-warn .log-badge { background: rgba(221,177,0,.2); color: var(--warn); }
.log-error .log-badge { background: rgba(241,76,76,.2); color: var(--err); }
.log-error .log-text { color: var(--err); }
.log-warn .log-text { color: var(--warn); }
.log-text { flex: 1; word-break: break-word; }
.log-src { color: var(--text-dim); font-size: 11px; flex-shrink: 0; }

.debugger { flex: 1; overflow-y: auto; padding: 8px; font-size: 12px; min-height: 0; -webkit-overflow-scrolling: touch; }
.dbg-status { padding: 4px 6px 8px; color: var(--text-dim); }
.dbg-badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; }
.dbg-badge.ok { color: var(--ok); background: rgba(78,201,176,.12); }
.dbg-badge.err { color: var(--err); background: rgba(241,76,76,.15); }
.dbg-section { font-size: 11px; color: var(--text-dim); margin: 8px 0 4px; letter-spacing: .3px; }
.dbg-ok { color: var(--ok); padding: 6px; }
.dbg-item {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 8px; border-radius: 5px; cursor: pointer; background: var(--panel2); margin-bottom: 4px;
}
.dbg-item:hover, .dbg-item:active { background: rgba(0,122,204,.15); }
.dbg-kind { font-size: 10px; padding: 1px 6px; border-radius: 4px; background: #3a3a3a; color: var(--warn); }
.dbg-kind.err { background: rgba(241,76,76,.2); color: var(--err); }
.dbg-msg { flex: 1; }
.dbg-loc { color: var(--accent); font-family: var(--mono); font-size: 11px; }
.dbg-stack { width: 100%; background: var(--bg); padding: 6px; border-radius: 4px; color: var(--text-dim); font-family: var(--mono); font-size: 11px; white-space: pre-wrap; margin: 4px 0 0; }

/* ===== preview ===== */
.preview {
  width: 40%; min-width: 260px; background: var(--panel);
  border-inline-end: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; min-height: 0;
}
.render-note { background: rgba(241,76,76,.15); color: var(--err); font-size: 12px; padding: 6px 10px; border-bottom: 1px solid var(--border); }
.preview-frame { flex: 1; border: none; background: #fff; width: 100%; }
.preview-reopen {
  align-self: stretch; writing-mode: vertical-rl; background: var(--panel);
  border: none; border-inline-end: 1px solid var(--border); color: var(--text-dim);
  cursor: pointer; padding: 12px 4px; font-family: var(--fa);
}
.preview-reopen:hover { color: #fff; background: var(--panel2); }

/* ===== status bar ===== */
.statusbar {
  display: flex; align-items: center; gap: 16px; background: var(--accent);
  color: #fff; font-size: 12px; padding: 4px 14px; flex-shrink: 0; flex-wrap: wrap;
}
.sb-spacer { flex: 1; }
.remix-link { color: #fff; text-decoration: none; opacity: .9; }
.remix-link:hover { opacity: 1; text-decoration: underline; }

/* ===== modal ===== */
.modal-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px;
}
.modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 24px; width: 360px; max-width: 92vw; animation: modalIn .2s ease;
}
@keyframes modalIn { from { opacity: 0; transform: scale(.96); } to { opacity: 1; transform: scale(1); } }
.modal h3 { margin: 0 0 10px; color: #fff; }
.modal-hint { font-size: 12px; color: var(--text-dim); margin: 0 0 12px; line-height: 1.7; }
.sc-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
kbd {
  background: var(--bg); border: 1px solid var(--border); border-radius: 4px;
  padding: 2px 8px; font-family: var(--mono); font-size: 12px; color: var(--accent);
  direction: ltr;
}
.modal-close {
  margin-top: 16px; width: 100%; background: var(--accent); color: #fff;
  border: none; border-radius: 6px; padding: 12px; cursor: pointer; font-size: 15px;
}
.modal-close:hover { background: var(--accent2); }

/* ===== toast ===== */
.toast {
  position: fixed; bottom: 40px; left: 50%; transform: translateX(-50%);
  background: var(--panel2); color: #fff; padding: 10px 20px; border-radius: 8px;
  border: 1px solid var(--border); z-index: 200; box-shadow: 0 6px 24px rgba(0,0,0,.5);
  animation: toastIn .3s ease; max-width: 90vw; text-align: center;
}
.toast-mobile { bottom: calc(72px + var(--safe-b)); }
.toast-success { border-color: var(--ok); }
.toast-error { border-color: var(--err); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #424242; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* ============================================================
   MOBILE (< 900px) : single-view + bottom tab nav
   ============================================================ */
.mbody {
  flex: 1 1 auto; display: flex; flex-direction: column;
  min-height: 0; overflow: hidden; position: relative;
}
.mbody > .explorer,
.mbody > .center,
.mbody > .preview,
.mbody > .bottom {
  width: 100%; flex: 1 1 auto; border: none; min-width: 0; min-height: 0;
  animation: viewIn .2s ease;
}
@keyframes viewIn { from { opacity: .4; } to { opacity: 1; } }

.mbody > .explorer { display: flex; }
.mbody > .center { display: flex; }
.mbody > .preview { display: flex; }
.mbody > .bottom { display: flex; height: auto; }

/* file explorer FABs on mobile */
.fab-row { display: flex; gap: 8px; padding: 10px; flex-shrink: 0; }
.fab {
  flex: 1; background: var(--accent); color: #fff; border: none;
  border-radius: 10px; padding: 14px; font-size: 14px; font-family: var(--fa);
  cursor: pointer; min-height: 48px;
}
.fab:active { background: var(--accent2); transform: scale(.97); }
.fab-alt { background: var(--panel2); color: var(--text); border: 1px solid var(--border); }
.preview-refresh { margin: 10px; flex: none; }

/* ===== mobile bottom navigation ===== */
.mobile-nav {
  display: flex; align-items: stretch; background: var(--panel);
  border-top: 1px solid var(--border); flex-shrink: 0;
  padding-bottom: var(--safe-b);
}
.mnav {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; background: transparent; border: none; color: var(--text-dim);
  font-size: 11px; font-family: var(--fa); padding: 9px 4px; cursor: pointer;
  border-top: 2px solid transparent; transition: color .15s, background .15s;
  min-height: 56px;
}
.mnav .mnav-icon { font-size: 20px; line-height: 1; position: relative; }
.mnav-badge {
  position: absolute; top: -6px; right: -12px; background: var(--err); color: #fff;
  font-size: 9px; min-width: 15px; height: 15px; padding: 0 3px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center; font-family: var(--fa);
}
.mnav-label { font-size: 11px; }
.mnav.active { color: #fff; background: rgba(0,122,204,.14); border-top-color: var(--accent); }
.mnav:active { background: rgba(0,122,204,.22); }

/* ===== mobile overflow bottom-sheet ===== */
.sheet-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; z-index: 300; animation: fadeIn .2s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: 100%; background: var(--panel); border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0; padding: 8px 16px calc(16px + var(--safe-b));
  animation: sheetUp .28s cubic-bezier(.2,.8,.2,1);
}
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-grip { width: 42px; height: 4px; background: #555; border-radius: 3px; margin: 6px auto 12px; }
.sheet-title { font-size: 13px; color: var(--text-dim); text-align: center; margin-bottom: 14px; }
.sheet-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.sb {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 12px;
  color: var(--text); padding: 14px 6px; cursor: pointer; min-height: 74px; font-family: var(--fa);
}
.sb:active { background: var(--accent); color: #fff; transform: scale(.96); }
.sb-icon { font-size: 22px; }
.sb-label { font-size: 12px; }
.sheet-remix { display: block; text-align: center; color: var(--text-dim); text-decoration: none; font-size: 12px; margin: 16px 0 8px; }
.sheet-close {
  width: 100%; background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 12px; cursor: pointer; font-size: 15px; margin-top: 4px;
}
.sheet-close:active { background: var(--panel2); }

/* ===== tablet tweaks ===== */
@media (min-width: 900px) and (max-width: 1100px) {
  .preview { width: 34%; min-width: 220px; }
  .explorer { width: 190px; }
}

/* ===== mobile-specific overrides ===== */
@media (max-width: 899px) {
  .topbar { padding: calc(6px + var(--safe-t)) 10px 6px; gap: 8px; flex-wrap: nowrap; }
  .statusbar { gap: 8px; padding: 4px 10px; font-size: 11px; }
  .bottom { height: auto; border-top: none; }
  /* prevent iOS zoom on focus: inputs >= 16px */
  .code-ta { font-size: 16px; line-height: 24px; }
  .code-pre { font-size: 16px; line-height: 24px; }
  .gutter { font-size: 16px; line-height: 24px; width: 42px; }
  .gutter-line { height: 24px; }
}

/* very small phones */
@media (max-width: 400px) {
  .brand > span:nth-child(2) { font-size: 16px; }
  .mnav-label { font-size: 10px; }
  .sheet-grid { gap: 8px; }
  .sb { padding: 12px 4px; min-height: 68px; }
}