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

:root {
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --radius: 16px;
  --radius-sm: 12px;
  --gap: 8px;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg: #0c0c0e;
  --surface: #18181b;
  --surface-2: #1e1e22;
  --surface-btn: #27272a;
  --surface-btn-hover: #303033;
  --surface-num: #2a2a2e;
  --surface-num-hover: #343438;
  --border: rgba(255,255,255,0.06);
  --border-display: rgba(255,255,255,0.08);
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #52525b;
  --accent: #f97316;
  --accent-hover: #ea580c;
  --accent-subtle: rgba(249,115,22,0.12);
  --accent-text: #fb923c;
  --fn-text: #d4d4d8;
  --display-bg: #111113;
  --shadow: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
  --header-logo-hi-filter: brightness(0) invert(1);
}

[data-theme="light"] {
  --bg: #f4f4f5;
  --surface: #ffffff;
  --surface-2: #f9f9fa;
  --surface-btn: #e4e4e7;
  --surface-btn-hover: #d4d4d8;
  --surface-num: #ffffff;
  --surface-num-hover: #f4f4f5;
  --border: rgba(0,0,0,0.08);
  --border-display: rgba(0,0,0,0.1);
  --text: #18181b;
  --text-2: #71717a;
  --text-3: #a1a1aa;
  --accent: #f97316;
  --accent-hover: #ea580c;
  --accent-subtle: rgba(249,115,22,0.08);
  --accent-text: #ea580c;
  --fn-text: #3f3f46;
  --display-bg: #fafafa;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.08);
  --header-logo-hi-filter: none;
}

html {
  font-size: 16px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background var(--transition), color var(--transition);
  overflow-x: hidden;
  overflow-y: auto;
}

.hidden { display: none !important; }

.site-header {
  width: 100%;
  max-width: 400px;
  padding: 32px 12px 28px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.header-logo {
  height: 28px;
  width: auto;
  object-fit: contain;
  transition: filter var(--transition);
}

.header-logo-en {
  height: 14px;
}

.header-logo-hi {
  filter: var(--header-logo-hi-filter);
}

.calculator {
  width: 100%;
  max-width: 400px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.toolbar-spacer {
  flex: 1;
}

.tool-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 600;
}

.tool-btn:hover {
  background: var(--surface-btn);
  color: var(--text);
}

.lang-label {
  font-size: 0.75rem;
  line-height: 1;
}

.display-container {
  background: var(--display-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border-display);
  transition: background var(--transition), border-color var(--transition), box-shadow 0.5s ease;
  box-shadow: var(--shadow);
}

.display {
  padding: 20px 20px 16px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-height: 100px;
  justify-content: flex-end;
}

.expression {
  font-size: 0.85rem;
  color: var(--text-3);
  height: 22px;
  line-height: 22px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  direction: rtl;
  unicode-bidi: plaintext;
  width: 100%;
  text-align: right;
  font-weight: 400;
}

.result {
  font-size: 2.75rem;
  font-weight: 300;
  line-height: 1.2;
  color: var(--text);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 100%;
  text-align: right;
  letter-spacing: -0.02em;
  transition: color var(--transition);
}

.result.error {
  color: #ef4444;
}

.keyboard {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
}

.btn {
  position: relative;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  min-height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: background var(--transition), transform 0.1s ease, color var(--transition);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  outline: none;
}

.btn::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0;
  background: radial-gradient(circle at var(--ripple-x, 50%) var(--ripple-y, 50%), var(--accent-subtle) 0%, transparent 60%);
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.btn.ripple::after {
  opacity: 1;
  animation: ripple-fade 0.35s ease-out forwards;
}

@keyframes ripple-fade {
  0% { opacity: 0.7; transform: scale(0.5); }
  100% { opacity: 0; transform: scale(1.5); }
}

.btn:active {
  transform: scale(0.95);
}

.btn sup {
  font-size: 0.6em;
  vertical-align: super;
  line-height: 0;
}

.num {
  background: var(--surface-num);
  color: var(--text);
  font-size: 1.25rem;
  font-weight: 400;
  border: 1px solid var(--border);
}

.num:hover {
  background: var(--surface-num-hover);
}

.fn {
  background: var(--surface-btn);
  color: var(--fn-text);
  font-size: 0.85rem;
  font-weight: 500;
  border: 1px solid var(--border);
}

.fn:hover {
  background: var(--surface-btn-hover);
}

.accent-text {
  color: var(--accent-text);
}

.mode {
  background: var(--surface-btn);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  border: 1px solid var(--border);
}

.mode:hover {
  background: var(--surface-btn-hover);
}

.op {
  background: var(--accent);
  color: #fff;
  font-size: 1.3rem;
  font-weight: 500;
  border: none;
}

.op:hover {
  background: var(--accent-hover);
}

.op::after {
  background: radial-gradient(circle at var(--ripple-x, 50%) var(--ripple-y, 50%), rgba(255,255,255,0.3) 0%, transparent 60%);
}

.equals {
  background: var(--accent);
}

.zero {
  grid-column: span 4;
  border-radius: var(--radius-sm);
  justify-content: flex-start;
  padding-left: 24px;
}

.footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 80px 0 4px;
  font-size: 0.7rem;
  color: var(--text-3);
  letter-spacing: 0.02em;
}

.footer-logo {
  height: 16px;
  width: auto;
  opacity: 0.7;
  transition: opacity var(--transition);
}

.footer-logo:hover {
  opacity: 1;
}

[data-theme="dark"] .logo-for-light { display: none; }
[data-theme="dark"] .logo-for-dark { display: inline; }
[data-theme="light"] .logo-for-light { display: inline; }
[data-theme="light"] .logo-for-dark { display: none; }

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border-display);
  padding: 16px;
  transition: background var(--transition), border-color var(--transition);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.panel-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 12px;
}

.panel-header .panel-title {
  margin-bottom: 0;
}

.panel-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border-display);
  background: var(--display-bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9rem;
  outline: none;
  transition: border-color var(--transition), background var(--transition);
  margin-bottom: 8px;
}

.panel-input:focus {
  border-color: var(--accent);
}

.panel-input::placeholder {
  color: var(--text-3);
}

.panel-btn {
  width: 100%;
  padding: 10px 16px;
  border-radius: 10px;
  border: none;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition);
  margin-bottom: 6px;
}

.panel-btn.primary {
  background: var(--accent);
  color: #fff;
}

.panel-btn.primary:hover {
  background: var(--accent-hover);
}

.panel-btn.secondary {
  background: var(--surface-btn);
  color: var(--text-2);
  border: 1px solid var(--border);
}

.panel-btn.secondary:hover {
  background: var(--surface-btn-hover);
}

.panel-btn-sm {
  padding: 6px 12px;
  border-radius: 8px;
  border: none;
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
}

.panel-btn-sm.danger {
  background: rgba(239,68,68,0.12);
  color: #ef4444;
}

.panel-btn-sm.danger:hover {
  background: rgba(239,68,68,0.2);
}

.auth-step {
  display: flex;
  flex-direction: column;
}

.auth-message {
  font-size: 0.8rem;
  color: var(--accent-text);
  margin-top: 4px;
  text-align: center;
}

.auth-message.error-msg {
  color: #ef4444;
}

.auth-user-email {
  font-size: 0.9rem;
  color: var(--text);
  font-weight: 500;
  margin-bottom: 12px;
  word-break: break-all;
}

.dev-code {
  background: var(--accent-subtle);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.8rem;
  color: var(--accent-text);
  margin-bottom: 8px;
  text-align: center;
  font-weight: 600;
}

.history-list {
  max-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--display-bg);
  border: 1px solid var(--border);
  transition: background var(--transition);
}

.history-item-content {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.history-item-expr {
  font-size: 0.75rem;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-result {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item-meta {
  display: flex;
  align-items: center;
  gap: 4px;
}

.history-badge {
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--accent-text);
  background: var(--accent-subtle);
  padding: 2px 5px;
  border-radius: 4px;
  letter-spacing: 0.04em;
}

.history-delete-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}

.history-delete-btn:hover {
  background: rgba(239,68,68,0.12);
  color: #ef4444;
}

.history-empty,
.history-login-prompt {
  font-size: 0.8rem;
  color: var(--text-3);
  text-align: center;
  padding: 16px 0;
}

.tool-btn.active {
  background: var(--accent-subtle);
  color: var(--accent-text);
  border-color: var(--accent);
}

@keyframes diya-glow {
  0% { box-shadow: var(--shadow), 0 0 20px var(--accent-subtle); }
  50% { box-shadow: var(--shadow), 0 0 40px var(--accent-subtle), 0 0 80px rgba(249,115,22,0.08); }
  100% { box-shadow: var(--shadow); }
}

.display-container.diya-flash {
  animation: diya-glow 0.5s ease-out forwards;
}

@media (max-width: 440px) {
  .site-header {
    padding: 24px 8px 12px;
  }
  .calculator {
    padding: 0 8px 8px;
    gap: 6px;
  }
  .keyboard {
    gap: 6px;
  }
  .result {
    font-size: 2.2rem;
  }
  .btn {
    border-radius: 10px;
    min-height: 44px;
  }
  .fn { font-size: 0.78rem; }
  .num { font-size: 1.15rem; }
  .op { font-size: 1.15rem; }
}

@media (max-width: 360px) {
  .keyboard { gap: 5px; }
  .result { font-size: 1.9rem; }
  .fn { font-size: 0.72rem; }
  .btn { min-height: 40px; }
}
