:root {
  color-scheme: dark;
  --app-height: 100dvh;
  --safe-top: min(env(safe-area-inset-top, 0px), 54px);
  --safe-bottom: min(env(safe-area-inset-bottom, 0px), 34px);
  --bg: #0d1117;
  --bg-deep: #090d13;
  --surface: #111824;
  --surface-raised: #171f2b;
  --surface-hover: #1c2634;
  --header: #0b111b;
  --border: #2a3544;
  --border-strong: #3a4a5f;
  --text: #f0f3f7;
  --text-muted: #98a4b3;
  --text-faint: #6f7b89;
  --blue: #5794ff;
  --blue-soft: #162b4e;
  --green: #63c847;
  --green-soft: #16351c;
  --yellow: #e3b341;
  --red: #ff7168;
  --red-soft: #3b1d20;
  --sheet-radius: 24px;
  --touch: 44px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

/* Authentication */
#login-screen {
  width: 100%;
  height: var(--app-height);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 34px;
  padding: max(24px, var(--safe-top)) 24px max(24px, var(--safe-bottom));
  background: var(--bg);
}

.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.login-brand > i {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  margin-bottom: 16px;
  border: 1px solid #315489;
  border-radius: 15px;
  background: var(--blue-soft);
  color: #7aa9ff;
  font-size: 25px;
}

.login-brand h1 {
  color: var(--text);
  font-size: 28px;
  line-height: 1.15;
  font-weight: 720;
  letter-spacing: -0.03em;
}

.login-brand p {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.4;
}

.login-form {
  width: min(100%, 360px);
  display: flex;
  flex-direction: column;
}

.login-form label {
  margin: 0 0 7px 2px;
  color: #c7d0dc;
  font-size: 13px;
  font-weight: 600;
}

.login-form input {
  width: 100%;
  height: 50px;
  padding: 0 15px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  appearance: none;
}

.login-form input + label {
  margin-top: 15px;
}

.login-form input:focus {
  border-color: var(--blue);
}

#login-error {
  min-height: 20px;
  margin: 8px 0;
  color: var(--red);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
}

#login-btn {
  min-height: 50px;
  border: 1px solid #2d8d42;
  border-radius: 11px;
  background: #238636;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

#login-btn:active {
  background: #2ea043;
}

#login-btn:disabled {
  cursor: wait;
  opacity: 0.6;
}

/* App shell */
#app {
  width: 100%;
  height: var(--app-height);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  background: var(--bg);
  overflow: hidden;
}

#app-header {
  grid-row: 1;
  display: grid;
  grid-template-columns: auto minmax(90px, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: calc(58px + var(--safe-top));
  padding: max(10px, var(--safe-top)) 14px 10px;
  border-bottom: 1px solid #202a38;
  background: var(--header);
}

#app-header h1 {
  color: #f5f7fa;
  font-size: 17px;
  line-height: 1;
  font-weight: 720;
  letter-spacing: -0.025em;
  white-space: nowrap;
}

.current-tab-button {
  min-width: 0;
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-raised);
  color: #d7dde6;
  cursor: pointer;
}

.current-tab-button:active {
  background: var(--surface-hover);
}

.current-tab-button > i:first-child {
  color: var(--blue);
  font-size: 14px;
}

.current-tab-button > i:last-child {
  color: var(--text-muted);
  font-size: 11px;
}

#current-tab-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}

.connection-status {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.connection-status i {
  color: var(--text-faint);
  font-size: 8px;
}

.connection-status.connected {
  color: #79d862;
}

.connection-status.connected i {
  color: var(--green);
}

.connection-status.warming {
  color: var(--yellow);
}

.connection-status.warming i {
  color: var(--yellow);
}

.connection-status.error {
  color: var(--red);
}

.connection-status.error i {
  color: var(--red);
}

#error-banner {
  grid-row: 2;
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 12px;
  border-bottom: 1px solid #6c2c31;
  background: var(--red-soft);
  color: #ffb2ac;
  font-size: 12px;
  line-height: 1.4;
}

#error-banner.visible {
  display: flex;
}

#error-banner button {
  min-height: 34px;
  flex: 0 0 auto;
  padding: 0 12px;
  border: 1px solid #934047;
  border-radius: 8px;
  background: #572329;
  color: #ffd3d0;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

#terminal-region {
  grid-row: 3;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: var(--bg);
}

#terminal-host {
  width: 100%;
  height: 100%;
  padding: 7px 5px 3px;
}

#terminal-host .terminal,
#terminal-host .xterm {
  width: 100%;
  height: 100%;
}

#terminal-host .xterm-viewport {
  scrollbar-color: #344050 transparent;
  scrollbar-width: thin;
}

#loading {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 13px;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 13px;
}

.spinner {
  color: var(--blue);
  font-size: 29px;
  animation: spin 0.8s linear infinite;
}

.spin-icon {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#terminal-toolbar {
  grid-row: 4;
  min-height: calc(52px + var(--safe-bottom));
  display: grid;
  grid-template-columns: repeat(3, minmax(45px, 1fr)) 1px repeat(4, minmax(42px, 0.8fr));
  align-items: start;
  gap: 6px;
  padding: 7px 8px max(7px, var(--safe-bottom));
  border-top: 1px solid #202a38;
  background: var(--bg-deep);
}

#terminal-toolbar button {
  min-width: 0;
  min-height: var(--touch);
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-raised);
  color: #dfe5ed;
  font-size: 13px;
  font-weight: 620;
  cursor: pointer;
}

#terminal-toolbar button:active,
#terminal-toolbar button.active {
  border-color: #4c79bd;
  background: var(--blue-soft);
  color: #8eb7ff;
}

#terminal-toolbar button i {
  font-size: 17px;
}

.toolbar-separator {
  width: 1px;
  height: 28px;
  align-self: center;
  background: var(--border-strong);
}

/* Dialogs and bottom sheets */
.dialog-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.dialog-scrim {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: rgba(2, 5, 9, 0.72);
  cursor: default;
}

.bottom-sheet {
  width: min(100%, 560px);
  max-height: min(82dvh, 740px);
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 0 14px max(14px, var(--safe-bottom));
  border: 1px solid #354254;
  border-bottom: 0;
  border-radius: var(--sheet-radius) var(--sheet-radius) 0 0;
  background: var(--surface);
  box-shadow: 0 -20px 55px rgba(0, 0, 0, 0.44);
  animation: sheet-in 180ms ease-out;
}

.bottom-sheet:focus,
.confirm-dialog:focus {
  outline: none;
}

@keyframes sheet-in {
  from { transform: translateY(18px); opacity: 0.65; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-header {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex: 0 0 auto;
  padding: 17px 2px 13px;
}

.sheet-header h2 {
  color: #f4f6f9;
  font-size: 24px;
  line-height: 1.15;
  font-weight: 750;
  letter-spacing: -0.03em;
}

.sheet-header p {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.35;
}

.sheet-header p i {
  margin-right: 4px;
}

.icon-button {
  width: var(--touch);
  height: var(--touch);
  flex: 0 0 var(--touch);
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface-hover);
  color: #d5dce5;
  cursor: pointer;
}

.icon-button:active {
  background: #253244;
}

.notice,
.inline-error {
  margin-bottom: 10px;
  padding: 10px 12px;
  border: 1px solid #705925;
  border-radius: 9px;
  background: #2f2818;
  color: #e8c66d;
  font-size: 12px;
  line-height: 1.4;
}

.inline-error {
  border-color: #793238;
  background: var(--red-soft);
  color: #ffada6;
}

.sessions-list {
  min-height: 78px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1px 1px 8px;
}

.session-row {
  width: 100%;
  min-height: 60px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px 44px;
  align-items: center;
  gap: 10px;
  padding: 8px 7px 8px 11px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: #131b26;
  color: var(--text);
  text-align: left;
}

.session-row.active {
  border-color: var(--blue);
  background: #142238;
  box-shadow: inset 0 0 0 1px rgba(87, 148, 255, 0.16);
}

.session-select {
  min-width: 0;
  min-height: 44px;
  grid-column: 1;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.session-icon {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #101720;
  color: var(--text-muted);
  font-size: 16px;
}

.session-row.active .session-icon {
  border-color: #476fa9;
  background: var(--blue-soft);
  color: #80adff;
}

.session-copy {
  min-width: 0;
}

.session-copy strong,
.session-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-copy strong {
  color: #f2f5f8;
  font-size: 15px;
  line-height: 1.35;
  font-weight: 680;
}

.session-copy small {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 12px;
}

.session-copy small i {
  margin-right: 5px;
  color: var(--text-faint);
  font-size: 7px;
  vertical-align: 1px;
}

.session-copy small.codex-status {
  color: #79d862;
}

.session-copy small.codex-status i {
  color: var(--green);
}

.session-menu-button,
.session-close-button {
  width: var(--touch);
  height: var(--touch);
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.session-menu-button:active,
.session-close-button:active {
  background: #273344;
  color: var(--text);
}

.session-close-button:active {
  color: var(--red);
}

.session-close-button {
  border: 1px solid var(--border);
  background: var(--surface-hover);
}

.sheet-primary-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  flex: 0 0 auto;
  padding: 8px 1px 10px;
  border-top: 1px solid #263141;
}

.action-card {
  min-height: 54px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 11px;
  border: 1px solid #385f96;
  border-radius: 12px;
  background: #132238;
  color: #7cafff;
  text-align: left;
  cursor: pointer;
}

.action-card > i {
  flex: 0 0 auto;
  font-size: 22px;
}

.action-card span {
  min-width: 0;
}

.action-card strong,
.action-card small {
  display: block;
}

.action-card strong {
  color: #8db8ff;
  font-size: 13px;
  line-height: 1.25;
  font-weight: 700;
}

.action-card small {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 10px;
  line-height: 1.3;
}

.action-card-codex {
  border-color: #3f7536;
  background: #142a1a;
  color: #78d45d;
}

.action-card-codex strong {
  color: #80dc66;
}

.action-card:active {
  filter: brightness(1.16);
}

.action-card:disabled {
  cursor: not-allowed;
  filter: none;
  opacity: 0.42;
}

.sheet-footer {
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
  border-top: 1px solid #263141;
}

.quiet-button,
.quiet-danger-button {
  min-height: var(--touch);
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 620;
  cursor: pointer;
}

.quiet-danger-button {
  color: #ee8981;
}

.quiet-button i,
.quiet-danger-button i {
  margin-right: 5px;
}

/* Codex launcher */
.wizard-sheet {
  max-height: min(90dvh, 780px);
}

.wizard-header {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
}

.wizard-header > div {
  min-width: 0;
}

.wizard-header h2 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 20px;
}

.wizard-content {
  min-height: 190px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 2px 1px 10px;
}

.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.launcher-choice {
  min-height: 150px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 16px;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  background: #131b26;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.launcher-choice > i {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  margin-bottom: 20px;
  border: 1px solid #426da6;
  border-radius: 11px;
  background: var(--blue-soft);
  color: #7aa9ff;
  font-size: 20px;
}

.launcher-choice strong {
  font-size: 15px;
  line-height: 1.25;
}

.launcher-choice small {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.4;
}

.launcher-choice:active {
  border-color: var(--blue);
  background: #172337;
}

.wizard-search,
.field-input {
  width: 100%;
  height: 46px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0f1620;
  color: var(--text);
  font-size: 16px;
  appearance: none;
}

.wizard-search {
  margin-bottom: 10px;
}

.wizard-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.wizard-list-button {
  width: 100%;
  min-height: 58px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: #131b26;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.wizard-list-button > i:first-child {
  color: #7baaff;
  font-size: 18px;
}

.wizard-list-button > i:last-child {
  color: var(--text-faint);
  font-size: 13px;
}

.wizard-list-button strong,
.wizard-list-button small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.wizard-list-button strong {
  font-size: 14px;
  font-weight: 650;
}

.wizard-list-button small {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 11px;
}

.wizard-list-button:active {
  border-color: var(--blue);
  background: #172337;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 150px;
  padding: 20px;
  color: var(--text-muted);
  text-align: center;
}

.empty-state i {
  color: var(--text-faint);
  font-size: 29px;
}

.empty-state strong {
  color: #dbe1e9;
  font-size: 14px;
}

.empty-state p {
  max-width: 280px;
  font-size: 12px;
  line-height: 1.45;
}

.launcher-form {
  display: flex;
  flex-direction: column;
  gap: 13px;
}

.field-group label {
  display: block;
  margin: 0 0 6px 2px;
  color: #cbd3dd;
  font-size: 12px;
  font-weight: 650;
}

.field-group p {
  margin: 5px 2px 0;
  color: var(--text-faint);
  font-size: 10px;
  line-height: 1.35;
}

.selection-summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: #101720;
}

.selection-summary > i {
  color: #7baaff;
  font-size: 18px;
}

.selection-summary strong,
.selection-summary small {
  display: block;
}

.selection-summary strong {
  font-size: 13px;
}

.selection-summary small {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 11px;
}

.primary-button {
  min-height: 50px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid #397332;
  border-radius: 11px;
  background: #1d5729;
  color: #f3fff0;
  font-size: 14px;
  font-weight: 720;
  cursor: pointer;
}

.primary-button:active {
  background: #277136;
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.branch-tag {
  display: inline-flex;
  align-items: center;
  min-height: 20px;
  padding: 0 7px;
  border-radius: 9px;
  background: #202b39;
  color: var(--text-muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.branch-tag.checked-out {
  background: #3a3018;
  color: #e5bd50;
}

/* Per-tab actions */
.action-sheet {
  max-height: none;
}

.action-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding-bottom: 4px;
}

.action-list button {
  width: 100%;
  min-height: 60px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: #131b26;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.action-list button > i {
  color: #7caaff;
  font-size: 19px;
}

.action-list button strong,
.action-list button small {
  display: block;
}

.action-list button strong {
  font-size: 14px;
}

.action-list button small {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 11px;
}

.action-list button.danger-action > i,
.action-list button.danger-action strong {
  color: var(--red);
}

.action-list button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

/* Confirm dialog */
.centered-layer {
  align-items: center;
  padding: 20px;
}

.confirm-dialog {
  width: min(100%, 350px);
  position: relative;
  z-index: 1;
  padding: 22px;
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  background: var(--surface-raised);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  text-align: center;
}

.confirm-dialog > i {
  color: var(--yellow);
  font-size: 30px;
}

.confirm-dialog h2 {
  margin-top: 12px;
  font-size: 19px;
}

.confirm-dialog p {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.5;
}

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 20px;
}

.confirm-actions button {
  min-height: 46px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 680;
  cursor: pointer;
}

.confirm-actions .quiet-button {
  border: 1px solid var(--border);
  background: #121923;
  color: #d2d9e2;
}

.danger-button {
  border: 1px solid #8f353d;
  background: #5b2429;
  color: #ffd3cf;
}

@media (max-width: 360px) {
  #app-header {
    grid-template-columns: auto minmax(82px, 1fr) auto;
    gap: 7px;
    padding-left: 10px;
    padding-right: 10px;
  }

  #app-header h1 {
    font-size: 15px;
  }

  .connection-status span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .connection-status i {
    font-size: 10px;
  }

  .choice-grid {
    grid-template-columns: 1fr;
  }

  .launcher-choice {
    min-height: 112px;
  }

  .launcher-choice > i {
    margin-bottom: 12px;
  }
}

@media (min-width: 700px) {
  #app-header {
    padding-left: 22px;
    padding-right: 22px;
  }

  #terminal-host {
    padding: 10px;
  }

  #terminal-toolbar {
    grid-template-columns: repeat(3, 70px) 1px repeat(4, 56px);
    justify-content: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}
