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

:root {
  --sidebar-bg: #1a1d21;
  --input-bg: #222529;
  --input-border: rgba(255, 255, 255, 0.08);
  --text: #e8e8e8;
  --text-muted: #7a7a7a;
  --accent: #2684ff;
  --accent-hover: #4c9aff;
  --accent-dim: rgba(38, 132, 255, 0.25);
  --bg-tertiary: #2c2e33;
  --radius: 12px;
  --radius-lg: 14px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

body.theme-light {
  --sidebar-bg: #ffffff;
  --input-bg: #f5f5f5;
  --input-border: #e8e8e8;
  --text: #202124;
  --text-muted: #5f6368;
  --accent: #1a73e8;
  --accent-hover: #1765cc;
  --accent-dim: rgba(26, 115, 232, 0.12);
  --bg-tertiary: #e8eaed;
}

html {
  height: 100dvh;
  color-scheme: dark;
}

html, body {
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: -0.02em;
  line-height: 1.5;
  color: var(--text);
  background: var(--sidebar-bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body.theme-light { color-scheme: light; }

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}

.calc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  flex-shrink: 0;
}

.calc-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.calc-logo {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  flex-shrink: 0;
  box-shadow: var(--shadow);
  object-fit: cover;
}

body.theme-light .calc-logo {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.calc-title {
  font-size: 1.15rem;
  font-weight: 600;
}

.theme-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border-radius: var(--radius);
  border: 1px solid var(--input-border);
  background: var(--input-bg);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
  -webkit-tap-highlight-color: transparent;
}
.theme-btn:hover {
  background: var(--bg-tertiary);
  color: var(--accent);
}
.theme-btn:active { transform: scale(0.97); }

.theme-icon { font-size: 1.2rem; line-height: 1; }

.calc-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 16px 20px;
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.calc-display-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 16px 0 20px;
  min-height: 100px;
}

.calc-expression {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 0.95rem;
  color: var(--text-muted);
  text-align: right;
  min-height: 1.5em;
  margin-bottom: 4px;
  word-break: break-all;
}

.calc-display {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: clamp(2.25rem, 9vw, 3.25rem);
  font-weight: 500;
  text-align: right;
  line-height: 1.15;
  word-break: break-all;
}

.calc-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.calc-btn {
  min-height: 60px;
  border-radius: var(--radius-lg);
  border: none;
  font: inherit;
  font-size: 1.45rem;
  cursor: pointer;
  transition: background var(--transition), transform 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.calc-btn:active { transform: scale(0.97); }

.calc-btn-num {
  background: var(--bg-tertiary);
  color: var(--text);
  box-shadow: var(--shadow);
}
.calc-btn-num:hover { background: rgba(255, 255, 255, 0.08); }
body.theme-light .calc-btn-num {
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}
body.theme-light .calc-btn-num:hover { background: #f1f3f4; }

.calc-btn-fn {
  background: var(--input-bg);
  color: var(--text);
  font-size: 1.15rem;
}
.calc-btn-fn:hover { background: var(--bg-tertiary); }
body.theme-light .calc-btn-fn { background: #e8eaed; }
body.theme-light .calc-btn-fn:hover { background: #dadce0; }

.calc-btn-op {
  background: var(--accent-dim);
  color: var(--accent);
}
.calc-btn-op:hover {
  background: rgba(38, 132, 255, 0.35);
  color: var(--accent-hover);
}
body.theme-light .calc-btn-op:hover {
  background: rgba(26, 115, 232, 0.2);
}

.calc-btn-eq {
  background: var(--accent);
  color: #fff;
}
.calc-btn-eq:hover { background: var(--accent-hover); }

.calc-btn-wide { grid-column: span 2; }

@media (max-width: 380px) {
  .calc-main {
    padding-left: 12px;
    padding-right: 12px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
  .calc-buttons { gap: 8px; }
  .calc-btn {
    min-height: max(50px, 11vw);
    font-size: clamp(1.1rem, 4vw, 1.3rem);
  }
}

@media (max-height: 460px) {
  .calc-display-wrap { min-height: 64px; padding: 8px 0 12px; }
  .calc-display { font-size: clamp(1.5rem, 7vh, 2.25rem); }
  .calc-btn { min-height: 44px; font-size: 1.1rem; }
  .calc-buttons { gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .calc-btn, .theme-btn { transition: none; }
  .calc-btn:active, .theme-btn:active { transform: none; }
}
