/* ============================================
   Notepad Calculator — Style
   ============================================ */

/* --- Reset & Base --- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* Light theme (default) */
  --bg: #FAFAF7;
  --bg-editor: #FFFFFF;
  --bg-line-hover: rgba(0, 0, 0, 0.02);
  --bg-line-active: rgba(91, 127, 181, 0.06);
  --bg-header: #F5F5F0;
  --bg-summary: #F0F0EB;
  --bg-help: rgba(0, 0, 0, 0.5);
  --bg-help-panel: #FFFFFF;
  --bg-toast: #2d2d2d;

  --text: #2d2d2d;
  --text-muted: #888888;
  --text-faint: #bbbbbb;
  --text-result: #5b7fb5;
  --text-variable: #c47d4e;
  --text-comment: #999999;
  --text-error: #d4564e;
  --text-toast: #FFFFFF;

  --border: #e8e8e3;
  --border-active: #5b7fb5;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.15);

  --radius: 8px;
  --radius-lg: 12px;

  --font-mono: 'SF Mono', 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  --line-height: 40px;
  --line-num-width: 44px;
  --result-width: 160px;
}

/* --- Dark Theme --- */
[data-theme="dark"] {
  --bg: #111111;
  --bg-editor: #161616;
  --bg-line-hover: rgba(255, 255, 255, 0.03);
  --bg-line-active: rgba(126, 200, 200, 0.06);
  --bg-header: #1a1a1a;
  --bg-summary: #1a1a1a;
  --bg-help: rgba(0, 0, 0, 0.7);
  --bg-help-panel: #1e1e1e;
  --bg-toast: #e0e0e0;

  --text: #e0e0e0;
  --text-muted: #777777;
  --text-faint: #444444;
  --text-result: #7ec8c8;
  --text-variable: #d4a574;
  --text-comment: #555555;
  --text-error: #e07070;
  --text-toast: #1a1a2e;

  --border: #2a2a40;
  --border-active: #7ec8c8;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.4);
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  transition: background 0.3s ease, color 0.3s ease;
}

/* --- App Container --- */
.app {
  width: 100%;
  max-width: 800px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* --- Header --- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  transition: background 0.3s ease, border-color 0.3s ease;
}

.header-title {
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.btn-icon {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-family: var(--font-sans);
  font-weight: 500;
  transition: all 0.15s ease;
}

.btn-icon:hover {
  color: var(--text);
  border-color: var(--text-muted);
  background: var(--bg-line-hover);
}

/* Theme toggle icon visibility */
.icon { display: none; }
[data-theme="light"] .sun,
:root:not([data-theme="dark"]) .sun { display: block; }
[data-theme="dark"] .moon { display: block; }
[data-theme="dark"] .sun { display: none; }

/* --- Editor --- */
.editor {
  flex: 1;
  padding: 8px 0;
  background: var(--bg-editor);
  min-height: 400px;
  position: relative;
  transition: background 0.3s ease;
}

/* --- Line --- */
.line {
  display: flex;
  align-items: stretch;
  min-height: var(--line-height);
  padding: 0 16px 0 0;
  position: relative;
  transition: background 0.15s ease;
  border-left: 3px solid transparent;
}

.line:hover {
  background: var(--bg-line-hover);
}

.line.active {
  background: var(--bg-line-active);
  border-left-color: var(--border-active);
}

.line.is-comment {
  opacity: 0.6;
}

.line.has-error .expression {
  color: var(--text-error);
}

/* Line Number */
.line-num {
  width: var(--line-num-width);
  min-width: var(--line-num-width);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 12px;
  font-size: 12px;
  color: var(--text-faint);
  user-select: none;
  pointer-events: none;
  line-height: var(--line-height);
}

/* Expression Input */
.expression {
  flex: 1;
  min-width: 0;
  padding: 0 8px;
  font-family: var(--font-mono);
  font-size: 15px;
  line-height: var(--line-height);
  color: var(--text);
  border: none;
  outline: none;
  background: transparent;
  white-space: pre;
  overflow: hidden;
  caret-color: var(--border-active);
}

.expression:empty::before {
  content: attr(data-placeholder);
  color: var(--text-faint);
  pointer-events: none;
}

/* Result Display */
.result {
  width: var(--result-width);
  min-width: var(--result-width);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 4px;
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 500;
  line-height: var(--line-height);
  color: var(--text-result);
  user-select: none;
  cursor: pointer;
  border-radius: 4px;
  transition: background 0.15s ease, opacity 0.2s ease;
  opacity: 0;
}

.result.visible {
  opacity: 1;
}

.result:hover {
  background: var(--bg-line-hover);
}

.result.copied {
  animation: flash 0.4s ease;
}

@keyframes flash {
  0% { background: var(--border-active); color: white; }
  100% { background: transparent; }
}

/* Variable highlight in expressions */
.line.is-assignment .expression {
  color: var(--text);
}

/* --- Summary Bar --- */
.summary-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 20px;
  background: var(--bg-summary);
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0.03em;
  text-transform: uppercase;
  font-family: var(--font-sans);
  font-weight: 600;
  position: sticky;
  bottom: 0;
  z-index: 10;
  transition: background 0.3s ease, border-color 0.3s ease;
}

.summary-stat {
  white-space: nowrap;
}

.btn-clear {
  margin-left: auto;
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-clear:hover {
  color: var(--text-error);
  border-color: var(--text-error);
}

/* --- Help Overlay --- */
.help-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-help);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.2s ease;
}

.help-overlay[hidden] {
  display: none;
}

.help-panel {
  background: var(--bg-help-panel);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 90%;
  max-width: 520px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 24px;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.help-header h2 {
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.help-content section {
  margin-bottom: 20px;
}

.help-content h3 {
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.help-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 13px;
}

.help-row code,
.help-row kbd {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  background: var(--bg-line-hover);
  padding: 2px 6px;
  border-radius: 3px;
}

.help-row kbd {
  border: 1px solid var(--border);
}

.help-row span {
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 12px;
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-toast);
  color: var(--text-toast);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 6px;
  box-shadow: var(--shadow-md);
  z-index: 200;
  animation: toastIn 0.2s ease, toastOut 0.2s ease 1.3s forwards;
}

.toast[hidden] {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateX(-50%) translateY(0); }
  to { opacity: 0; transform: translateX(-50%) translateY(8px); }
}

/* --- Empty State --- */
.empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  z-index: 1;
  transition: opacity 0.3s ease;
}

.empty-state.hidden {
  opacity: 0;
}

.empty-hint {
  font-family: var(--font-sans);
  font-size: 16px;
  color: var(--text-faint);
  margin-bottom: 16px;
}

.empty-examples {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}

.empty-examples span {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-faint);
  opacity: 0.6;
}

/* --- Scrollbar --- */
.editor::-webkit-scrollbar {
  width: 6px;
}

.editor::-webkit-scrollbar-track {
  background: transparent;
}

.editor::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.help-panel::-webkit-scrollbar {
  width: 4px;
}

.help-panel::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

/* --- Responsive --- */
@media (max-width: 600px) {
  :root {
    --line-height: 36px;
    --line-num-width: 32px;
    --result-width: 110px;
  }

  .header {
    padding: 10px 12px;
  }

  .header-title {
    font-size: 12px;
  }

  .line {
    padding-right: 8px;
  }

  .expression {
    font-size: 14px;
  }

  .result {
    font-size: 13px;
  }

  .summary-bar {
    padding: 8px 12px;
    gap: 12px;
    font-size: 11px;
  }

  .help-panel {
    padding: 16px;
    width: 95%;
  }
}

@media (max-width: 400px) {
  :root {
    --result-width: 90px;
    --line-num-width: 28px;
  }

  .line-num {
    font-size: 10px;
    padding-right: 6px;
  }
}

/* --- Print --- */
@media print {
  .header, .summary-bar, .help-overlay, .toast, .empty-state {
    display: none;
  }

  .editor {
    min-height: auto;
  }

  .line {
    break-inside: avoid;
  }
}
