:root {
  --bg: #0d0d0f;
  --surface: #141418;
  --surface-2: #1c1c22;
  --border: #272730;
  --text: #f3f3f6;
  --muted: #9393a3;
  --accent: #d97757;
  --accent-soft: rgba(217, 119, 87, 0.16);
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --app-height: 100dvh;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  max-width: 100vw;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 100vw;
  height: var(--app-height);
  overflow: hidden;
  padding-top: env(safe-area-inset-top, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  background: var(--bg);
}

/* Top Bar */
.topbar {
  flex: 0 0 auto;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  width: 100%;
  max-width: 100vw;
  overflow: hidden;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--red);
  box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.22);
}
.status-dot.online {
  background: var(--green);
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.22);
}
.status-dot.warn {
  background: var(--yellow);
  box-shadow: 0 0 0 2px rgba(234, 179, 8, 0.22);
}

.segmented-tabs {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 2px;
  gap: 2px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.seg-btn.active {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border);
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  height: 30px;
  min-width: 30px;
  padding: 0 7px;
  border-radius: 7px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-pill {
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11.5px;
  font-weight: 700;
  height: 28px;
  padding: 0 9px;
  border-radius: 999px;
  cursor: pointer;
}

/* Workspace */
.workspace {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 100vw;
  position: relative;
  overflow: hidden;
}

.view {
  display: none;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.view.active {
  display: flex;
  flex-direction: column;
}

/* Session bar */
.session-bar {
  flex: 0 0 auto;
  height: 36px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  background: #101014;
  border-bottom: 1px solid var(--border);
  width: 100%;
  overflow: hidden;
}

.session-tabs {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 5px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  min-width: 0;
}
.session-tabs::-webkit-scrollbar {
  display: none;
}

.session-tab {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 9px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.session-tab.active {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text);
}

.session-tab .close-x {
  font-size: 11px;
  opacity: 0.65;
  padding: 0 2px;
}

.new-session-btn {
  flex: 0 0 auto;
  height: 26px;
  padding: 0 9px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

/* Terminal area */
.terminal-wrap {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  position: relative;
  overflow: hidden;
  background: var(--bg);
  padding: 4px 6px;
}

.terminal-mount {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.terminal-mount .xterm {
  height: 100%;
  width: 100%;
}

.terminal-mount .xterm-viewport {
  overflow-y: auto !important;
  overflow-x: hidden !important;
  -webkit-overflow-scrolling: touch;
}

.terminal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(13, 13, 15, 0.86);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

.overlay-card {
  width: 100%;
  max-width: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  text-align: center;
}

.overlay-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
}

.overlay-sub {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 14px;
  line-height: 1.45;
}

.overlay-sub code {
  color: var(--accent);
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}

/* Mobile quick-key bar */
.mobile-keys {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px)) 8px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 100vw;
}
.mobile-keys::-webkit-scrollbar {
  display: none;
}

.key-btn {
  flex: 0 0 auto;
  height: 34px;
  padding: 0 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}
.key-btn:active {
  transform: scale(0.95);
  background: #2b2b36;
}
.key-btn.accent {
  border-color: rgba(217, 119, 87, 0.5);
  color: var(--accent);
}
.key-btn.danger {
  border-color: rgba(239, 68, 68, 0.45);
  color: #f87171;
}
.key-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* OpenCodex 1:1 Iframe */
.ocx-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: var(--bg);
  display: block;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 8, 10, 0.82);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h2 {
  font-size: 16px;
  font-weight: 700;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 46px;
  height: 46px;
  border-radius: 12px;
}

.brand-row h1 {
  font-size: 19px;
  font-weight: 700;
}

.subtitle {
  font-size: 13px;
  color: var(--muted);
}

.login-form,
.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
}

input,
select {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  outline: none;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

.primary-btn {
  height: 42px;
  padding: 0 16px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.secondary-btn {
  height: 40px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.full {
  width: 100%;
}

.mode-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode-btn {
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.mode-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text);
}

.error-text {
  font-size: 12.5px;
  color: #f87171;
}

.ios-hint {
  font-size: 12px;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  line-height: 1.45;
}
