/* Reset and base */

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

:root {
  --bg:          #0f0f0f;
  --surface:     #1a1a1a;
  --surface2:    #242424;
  --border:      #2e2e2e;
  --text:        #e8e8e8;
  --text-muted:  #888;
  --accent:      #2d7cf6;
  --accent-dim:  #1a4a9a;
  --green:       #22c55e;
  --amber:       #f59e0b;
  --red:         #ef4444;
  --radius:      8px;
  --tab-h:       56px;
  --sidebar-w:   240px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* App shell */

#app {
  display: grid;
  grid-template-rows: 1fr var(--tab-h);
  grid-template-columns: 1fr;
  height: 100dvh;
}

#content {
  overflow: hidden;
  position: relative;
}

.tab-panel {
  display: none;
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tab-panel.active { display: flex; flex-direction: column; }

/* Bottom tab bar (mobile) */

#tabbar {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  padding: 6px 0;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
  min-height: 44px;
}

.tab-btn svg { width: 22px; height: 22px; }
.tab-btn.active { color: var(--accent); }

/* Chat tab */

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.relay  { background: var(--surface2); align-self: flex-start; }
.msg.user   { background: var(--accent-dim); align-self: flex-end; }
.msg.system { background: transparent; color: var(--text-muted); font-size: 12px; align-self: center; text-align: center; }

.msg b  { font-weight: 600; }
.msg i  { font-style: italic; color: #ccc; }
.msg code { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 13px; background: #111; padding: 1px 4px; border-radius: 3px; }

#chat-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

#chat-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  padding: 9px 12px;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.4;
  outline: none;
}

#chat-input:focus { border-color: var(--accent); }

#chat-send {
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 16px;
  cursor: pointer;
  white-space: nowrap;
  min-height: 44px;
}

#chat-send:disabled { background: var(--accent-dim); cursor: default; }

/* Approval buttons */
#approval-bar {
  display: none;
  padding: 10px 12px;
  gap: 10px;
  border-top: 1px solid var(--amber);
  background: rgba(245, 158, 11, 0.08);
}

#approval-bar.visible { display: flex; }

#approval-bar button {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  min-height: 48px;
}

#btn-yes { background: var(--green); color: #000; }
#btn-no  { background: var(--red);   color: #fff; }

#btn-view-file {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  align-self: center;
}

/* Project selector */
#project-bar {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  align-items: center;
  gap: 8px;
}

#project-select {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  padding: 7px 10px;
  outline: none;
}

/* Rescue tab */

#rescue-panel {
  padding: 12px;
  gap: 10px;
  overflow-y: auto;
}

.service-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.service-card.green { border-left: 3px solid var(--green); }
.service-card.amber { border-left: 3px solid var(--amber); }
.service-card.red   { border-left: 3px solid var(--red); }

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

.service-name  { font-weight: 600; font-size: 14px; }
.service-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  font-weight: 600;
}

.badge-green { background: rgba(34,197,94,0.15); color: var(--green); }
.badge-amber { background: rgba(245,158,11,0.15); color: var(--amber); }
.badge-red   { background: rgba(239,68,68,0.15);  color: var(--red); }

.service-detail { font-size: 12px; color: var(--text-muted); }

.service-action {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  padding: 8px 12px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  min-height: 44px;
}

.service-action:hover  { border-color: var(--accent); }
.service-action.danger { color: var(--red); border-color: var(--red); }

#rescue-refresh {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  padding: 10px;
  cursor: pointer;
  margin-bottom: 10px;
  width: 100%;
}

#rescue-timestamp { font-size: 11px; color: var(--text-muted); text-align: center; padding-bottom: 8px; }

/* Terminal tab */

#terminal-panel {
  background: #000;
  overflow: hidden;
}

#terminal-container {
  width: 100%;
  height: 100%;
}

.xterm { height: 100% !important; }
.xterm-viewport { overflow-y: auto !important; }

#chat-detail-panel { display: none; }

/* Tablet / desktop (>=768px) */

@media (min-width: 768px) {
  #app {
    grid-template-rows: 1fr;
    grid-template-columns: var(--sidebar-w) 1fr;
  }

  #tabbar {
    flex-direction: column;
    border-top: none;
    border-right: 1px solid var(--border);
    width: var(--sidebar-w);
    grid-row: 1;
    grid-column: 1;
    padding-top: 16px;
    gap: 4px;
    align-items: stretch;
    justify-content: flex-start;
  }

  .tab-btn {
    flex-direction: row;
    justify-content: flex-start;
    padding: 12px 20px;
    gap: 12px;
    font-size: 14px;
    border-radius: 0 var(--radius) var(--radius) 0;
    margin-right: 8px;
  }

  .tab-btn.active { background: var(--surface2); }

  #content {
    grid-row: 1;
    grid-column: 2;
  }
}

/* Desktop three-column (>=1100px) */

@media (min-width: 1100px) {
  :root { --sidebar-w: 280px; }

  #chat-panel {
    display: grid !important;
    grid-template-columns: 1fr 340px;
    grid-template-rows: auto 1fr auto auto;
  }

  #project-bar      { grid-column: 1 / -1; }
  #chat-messages    { grid-column: 1; grid-row: 2; }
  #chat-footer      { grid-column: 1; grid-row: 3; }
  #approval-bar     { grid-column: 1; grid-row: 4; }

  #chat-detail-panel {
    grid-column: 2;
    grid-row: 2 / -1;
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 16px;
    background: var(--surface);
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
}

/* Utility */

.hidden { display: none !important; }

.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: inline-block;
}

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

::-webkit-scrollbar       { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
