:root {
    --bg: #0f0f1a;
    --surface: #1a1a2e;
    --surface2: #252545;
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --text: #e2e8f0;
    --text-dim: #94a3b8;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --border: #2d2d50;
    --btn-radius: 10px;
  }

  * { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

  html, body {
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    touch-action: manipulation;
    /* Prevent iOS from resizing viewport when keyboard appears */
    position: fixed;
    width: 100%;
  }

  #app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    height: 100%; /* fallback for older browsers */
    max-height: 100dvh;
    overflow: hidden;
  }

  /* Header */
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    min-height: 64px;
  }

  /* Ghost cells */
  .ghost-cells {
    display: flex;
    gap: 3px;
    flex: 1;
  }
  .ghost-cell {
    width: 56px;
    height: 56px;
    border-radius: 8px;
    background: var(--bg);
    border: 2px solid var(--border);
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .ghost-cell.active { border-color: var(--primary); }
  .ghost-cell.empty {
    border-style: dashed;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-dim);
    font-size: 20px;
  }
  /* Preview screenshots */
  #pair-overlay .preview-scroll {
    display: flex;
    gap: 8px;
    margin: 10px 0;
    overflow-x: auto;
    max-width: 320px;
    padding: 4px 0;
    -webkit-overflow-scrolling: touch;
  }
  #pair-overlay .preview-scroll img {
    height: 160px;
    border-radius: 10px;
    border: 2px solid var(--border);
    flex-shrink: 0;
  }
  #pair-overlay .preview-scroll img:active {
    border-color: var(--primary);
  }
  /* Tutorial / Landing */
  #pair-overlay .tutorial {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin: 8px 0;
  }
  #pair-overlay .tutorial .step {
    background: var(--surface);
    border-radius: 14px;
    padding: 14px;
    border: 1px solid var(--border);
  }
  #pair-overlay .tutorial .step-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
  }
  #pair-overlay .tutorial .step-num {
    background: var(--primary);
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 700;
    flex-shrink: 0;
  }
  #pair-overlay .tutorial .step-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
  }
  #pair-overlay .tutorial .step-desc {
    font-size: 12px;
    color: var(--text-dim);
    line-height: 1.6;
    margin-bottom: 8px;
  }
  #pair-overlay .tutorial .step-img {
    width: 100%;
    border-radius: 8px;
    border: 1px solid var(--border);
  }
  #pair-overlay .tutorial .step-code {
    display: inline-block;
    background: #000;
    color: #10b981;
    padding: 4px 10px;
    border-radius: 6px;
    font-family: 'Menlo', monospace;
    font-size: 13px;
    margin: 4px 0;
  }
  #pair-overlay .section-title {
    font-size: 15px;
    font-weight: 700;
    color: var(--primary);
    margin-top: 8px;
  }
  #pair-overlay .pair-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 200px;
    color: var(--text-dim);
    font-size: 12px;
    margin: 12px 0;
  }
  #pair-overlay .pair-divider::before,
  #pair-overlay .pair-divider::after {
    content: '';
    flex: 1;
    border-top: 1px solid var(--border);
  }

  .ghost-cell canvas {
    width: 100%;
    height: 100%;
  }
  @keyframes claude-pulse {
    0%, 100% { box-shadow: 0 0 4px var(--primary); }
    50% { box-shadow: 0 0 16px var(--primary), 0 0 30px rgba(139,92,246,0.3); }
  }
  .claude-highlight {
    animation: claude-pulse 1.2s ease-in-out infinite;
    border: 1.5px solid var(--primary) !important;
  }
  .ghost-kill-btn {
    width: 28px;
    height: 28px;
    border: 1.5px solid #555;
    border-radius: 50%;
    background: transparent;
    color: #555;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    align-self: center;
    margin-left: 2px;
  }
  .ghost-kill-btn:active {
    color: #ef4444;
    border-color: #ef4444;
  }

  .logo {
    font-weight: 700;
    font-size: 15px;
    color: var(--primary);
  }

  .status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-dim);
  }

  .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--danger);
    transition: background 0.3s;
  }

  .status-dot.connected { background: var(--success); }

  .header-btns {
    display: flex;
    gap: 4px;
  }

  .header-btn {
    background: var(--surface2);
    border: none;
    color: var(--text-dim);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .header-btn:active { background: var(--primary); color: white; }

  /* Terminal */
  #terminal-container {
    flex: 1;
    min-height: 0;
    position: relative;
    background: #000;
    overflow: hidden;
  }

  #terminal-container .xterm {
    padding: 4px;
    height: 100%;
  }

  /* Controls */
  #controls {
    flex-shrink: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    transition: max-height 0.3s;
    overflow-y: auto;
  }

  #controls.collapsed {
    max-height: 0;
    padding: 0;
    overflow: hidden;
    border-top: none;
  }

  /* Quick Actions */
  .quick-row {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
    flex-wrap: wrap;
  }

  .qbtn {
    flex: 1;
    min-width: 48px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.1s, background 0.1s, border-color 0.1s;
    user-select: none;
  }

  .qbtn:active, .qbtn.tapped {
    transform: scale(0.93);
    background: var(--primary);
    border-color: var(--primary);
  }

  .qbtn.green { border-color: var(--success); color: var(--success); }
  .qbtn.green:active { background: var(--success); color: white; }
  .qbtn.red { border-color: var(--danger); color: var(--danger); }
  .qbtn.red:active { background: var(--danger); color: white; }
  .qbtn.amber { border-color: var(--warning); color: var(--warning); }
  .qbtn.amber:active { background: var(--warning); color: white; }

  /* D-Pad + Enter */
  .dpad-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    margin-bottom: 8px;
    position: relative;
  }

  .dpad {
    display: grid;
    grid-template-areas:
      ". up ."
      "left center right"
      ". down .";
    grid-template-columns: 52px 52px 52px;
    grid-template-rows: 46px 46px 46px;
    gap: 4px;
  }

  .dpad-btn {
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
  }

  .dpad-btn:active, .dpad-btn.tapped {
    transform: scale(0.9);
    background: var(--primary);
    border-color: var(--primary);
  }

  .dpad-btn.up { grid-area: up; }
  .dpad-btn.down { grid-area: down; }
  .dpad-btn.left { grid-area: left; }
  .dpad-btn.right { grid-area: right; }
  .dpad-btn.center {
    grid-area: center;
    background: var(--primary);
    border-color: var(--primary);
    color: white;
    font-size: 12px;
    font-weight: 700;
  }
  .dpad-btn.center:active { background: var(--primary-hover); }

  .side-btns {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 56px;
    flex-shrink: 0;
  }

  .side-btn {
    width: 100%;
    height: 44px;
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .side-btn:active, .side-btn.tapped {
    transform: scale(0.93);
    background: var(--primary);
    border-color: var(--primary);
  }

  /* Input Row */
  .input-row {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
  }

  .input-row input {
    flex: 1;
    height: 42px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    color: var(--text);
    font-size: 15px;
    padding: 0 12px;
    outline: none;
    font-family: 'Menlo', 'Courier New', monospace;
  }

  .input-row input:focus { border-color: var(--primary); }

  .send-btn {
    width: 64px;
    height: 42px;
    background: var(--primary);
    border: none;
    border-radius: var(--btn-radius);
    color: white;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
  }

  .send-btn:active { background: var(--primary-hover); }

  /* Upload Row */
  .upload-row {
    display: flex;
    gap: 6px;
  }

  .upload-btn {
    flex: 1;
    height: 42px;
    border: 1px dashed var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text-dim);
    font-size: 13px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
  }

  .upload-btn:active { border-color: var(--primary); color: var(--primary); }

  .upload-btn.uploading {
    border-color: var(--warning);
    color: var(--warning);
    animation: pulse 1s infinite;
  }

  @keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
  }

  .paste-glow {
    animation: pasteGlow 0.8s ease-in-out infinite;
  }
  @keyframes pasteGlow {
    0%, 100% { border-color: var(--primary); box-shadow: 0 0 4px var(--primary-glow); }
    50% { border-color: var(--success); box-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }
  }

  /* Toast */
  .toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px 20px;
    font-size: 13px;
    color: var(--text);
    z-index: 999;
    transition: transform 0.3s;
    white-space: nowrap;
    max-width: 90vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .toast.show { transform: translateX(-50%) translateY(0); }

  /* Hidden file input */
  #file-input { display: none; }

  /* Scrollbar */
  #controls::-webkit-scrollbar { width: 4px; }
  #controls::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

  /* Font size overlay */
  .font-overlay {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 20px 30px;
    z-index: 1000;
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .font-overlay.visible { display: flex; }

  .font-overlay .label { font-size: 14px; color: var(--text-dim); }
  .font-overlay .size { font-size: 28px; font-weight: 700; }

  .font-controls {
    display: flex;
    gap: 12px;
  }

  .font-controls button {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    font-size: 22px;
    cursor: pointer;
  }

  .font-controls button:active { background: var(--primary); }

  .backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
    display: none;
  }

  .backdrop.visible { display: block; }

  /* Claude menu popup */
  .claude-menu {
    display: none;
    position: fixed;
    bottom: 50%;
    left: 50%;
    transform: translate(-50%, 50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 12px;
    z-index: 1001;
    width: min(300px, 85vw);
    gap: 8px;
    flex-direction: column;
  }

  .claude-menu.visible { display: flex; }

  .claude-menu-btn {
    height: 50px;
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    user-select: none;
  }

  .claude-menu-btn:active {
    background: var(--primary);
    border-color: var(--primary);
  }

  .claude-menu-btn .desc {
    font-weight: 400;
    font-size: 11px;
    color: var(--text-dim);
  }

  .claude-menu-btn:active .desc { color: rgba(255,255,255,0.7); }

/* Numpad popup */
  .numpad-popup {
    display: none;
    position: fixed;
    bottom: 50%;
    left: 50%;
    transform: translate(-50%, 50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px;
    z-index: 1001;
    gap: 10px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
  }
  .numpad-popup.visible { display: flex; }
  .numpad-popup button {
    width: 60px;
    height: 60px;
    border: 1px solid var(--border);
    border-radius: var(--btn-radius);
    background: var(--surface2);
    color: var(--text);
    font-size: 24px;
    font-weight: 700;
    cursor: pointer;
    user-select: none;
  }
  .numpad-popup button:active {
    background: var(--primary);
    border-color: var(--primary);
  }

/* Select mode overlay */
  #select-overlay {
    display: none;
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 10;
    background: #000;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    -webkit-user-select: text;
    user-select: text;
  }
  #select-overlay pre {
    margin: 0;
    padding: 4px;
    color: #e2e8f0;
    font-size: inherit;
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    line-height: 1.2;
    white-space: pre;
    min-height: 100%;
  }
  #select-overlay .select-bar {
    position: sticky;
    top: 0;
    background: var(--primary);
    color: white;
    text-align: center;
    padding: 6px;
    font-size: 13px;
    font-weight: 600;
    z-index: 11;
  }
  #select-overlay .select-bar-bottom {
    position: sticky;
    bottom: 0;
    background: var(--primary);
    color: white;
    text-align: center;
    padding: 10px;
    font-size: 14px;
    font-weight: 700;
    z-index: 11;
  }

  /* Office view */
  #office-canvas {
    width: 100%;
    height: 100%;
  }

  .header-btn.active {
    background: var(--primary);
    color: white;
  }

  /* Pairing overlay */
  #pair-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10000;
    background: var(--bg);
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
    overflow-y: auto;
    padding: 20px 0 40px;
    -webkit-overflow-scrolling: touch;
  }
  #pair-overlay.visible { display: flex; }
  #pair-overlay .pair-title {
    font-size: 22px;
    font-weight: 700;
    color: var(--primary);
  }
  #pair-overlay .pair-desc {
    font-size: 14px;
    color: var(--text-dim);
    text-align: center;
    max-width: 280px;
  }
  #pair-overlay .pair-input {
    width: 200px;
    height: 56px;
    text-align: center;
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 8px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 12px;
    color: var(--text);
    outline: none;
    font-family: 'Menlo', monospace;
  }
  #pair-overlay .pair-input:focus { border-color: var(--primary); }
  #pair-overlay .pair-btn {
    width: 200px;
    height: 48px;
    background: var(--primary);
    border: none;
    border-radius: 12px;
    color: white;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
  }
  #pair-overlay .pair-btn:active { background: var(--primary-hover); }
  #pair-overlay .pair-btn:disabled { opacity: 0.5; }
  #pair-overlay .pair-status {
    font-size: 13px;
    color: var(--text-dim);
    min-height: 20px;
  }
  #pair-overlay .pair-status.error { color: var(--danger); }

  /* Disconnect overlay */
  #disconnect-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9000;
    background: rgba(15, 15, 26, 0.92);
    backdrop-filter: blur(6px);
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
  }
  #disconnect-overlay.visible { display: flex; }
  #disconnect-overlay .dc-icon {
    font-size: 48px;
    animation: dc-pulse 2s ease-in-out infinite;
  }
  @keyframes dc-pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.5; transform: scale(0.95); }
  }
  #disconnect-overlay .dc-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--danger);
  }
  #disconnect-overlay .dc-desc {
    font-size: 14px;
    color: var(--text-dim);
    text-align: center;
    max-width: 280px;
  }
  #disconnect-overlay .dc-retry {
    margin-top: 8px;
    padding: 12px 28px;
    background: var(--primary);
    border: none;
    border-radius: 12px;
    color: white;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
  }
  #disconnect-overlay .dc-retry:active { background: var(--primary-hover); }

  /* Session switch loading */
  #session-loading {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 50;
    background: rgba(0, 0, 0, 0.7);
    align-items: center;
    justify-content: center;
  }
  #session-loading.visible { display: flex; }
  .loading-spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }
  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  /* Toast notification for errors */
  #toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--surface);
    border: 1px solid var(--danger);
    border-radius: 12px;
    padding: 12px 20px;
    color: var(--text);
    font-size: 13px;
    max-width: 320px;
    text-align: center;
    z-index: 9500;
    opacity: 0;
    transition: transform 0.3s ease-out, opacity 0.3s ease-out;
    pointer-events: none;
  }
  #toast.visible {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
  }
