/* Modern Web Calculator Stylesheet */

/* Root Design Tokens */
:root {
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --transition-speed: 0.25s;

  /* Default Light Theme */
  --bg-gradient: linear-gradient(135deg, #e0eafc 0%, #cfdef3 100%);
  --card-bg: rgba(255, 255, 255, 0.88);
  --card-border: rgba(255, 255, 255, 0.6);
  --card-shadow: 0 20px 45px rgba(0, 0, 0, 0.1), 0 5px 15px rgba(0, 0, 0, 0.05);

  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --display-bg: #f8fafc;
  --display-border: #e2e8f0;

  --btn-num-bg: #ffffff;
  --btn-num-text: #1e293b;
  --btn-num-hover: #f1f5f9;
  --btn-num-active: #e2e8f0;
  --btn-num-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);

  --btn-fn-bg: #f1f5f9;
  --btn-fn-text: #334155;
  --btn-fn-hover: #e2e8f0;

  --btn-act-bg: #fee2e2;
  --btn-act-text: #ef4444;
  --btn-act-hover: #fecaca;

  --btn-op-bg: #e0e7ff;
  --btn-op-text: #4f46e5;
  --btn-op-hover: #c7d2fe;

  --btn-eq-bg: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  --btn-eq-text: #ffffff;
  --btn-eq-hover: linear-gradient(135deg, #4338ca 0%, #4f46e5 100%);
  --btn-eq-shadow: 0 4px 12px rgba(79, 70, 229, 0.35);

  --panel-bg: rgba(255, 255, 255, 0.95);
  --panel-border: #e2e8f0;
  --history-item-hover: #f8fafc;

  color-scheme: light;
}

/* Dark Theme Overrides */
[data-theme="dark"] {
  --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
  --card-bg: rgba(30, 41, 59, 0.85);
  --card-border: rgba(255, 255, 255, 0.08);
  --card-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.06);

  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --display-bg: #0f172a;
  --display-border: #334155;

  --btn-num-bg: #334155;
  --btn-num-text: #f8fafc;
  --btn-num-hover: #475569;
  --btn-num-active: #1e293b;
  --btn-num-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);

  --btn-fn-bg: #1e293b;
  --btn-fn-text: #cbd5e1;
  --btn-fn-hover: #334155;

  --btn-act-bg: rgba(239, 68, 68, 0.18);
  --btn-act-text: #f87171;
  --btn-act-hover: rgba(239, 68, 68, 0.28);

  --btn-op-bg: rgba(99, 102, 241, 0.2);
  --btn-op-text: #a5b4fc;
  --btn-op-hover: rgba(99, 102, 241, 0.32);

  --btn-eq-bg: linear-gradient(135deg, #6366f1 0%, #818cf8 100%);
  --btn-eq-text: #ffffff;
  --btn-eq-hover: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  --btn-eq-shadow: 0 4px 16px rgba(99, 102, 241, 0.45);

  --panel-bg: rgba(15, 23, 42, 0.96);
  --panel-border: #334155;
  --history-item-hover: #1e293b;

  color-scheme: dark;
}

/* Reset and Layout */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-family);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--bg-gradient);
  background-attachment: fixed;
  color: var(--text-primary);
  transition: background var(--transition-speed) ease, color var(--transition-speed) ease;
  padding: 16px;
  overflow-x: hidden;
}

.calculator-app {
  width: 100%;
  max-width: 380px;
  display: flex;
  justify-content: center;
}

/* Main Calculator Card */
.calculator-card {
  position: relative;
  width: 100%;
  background: var(--card-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--card-border);
  border-radius: 28px;
  box-shadow: var(--card-shadow);
  padding: 24px;
  overflow: hidden;
  transition: all var(--transition-speed) ease;
}

/* Bismillah */
.bismillah-title {
  text-align: center;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 14px;
  line-height: 1.4;
  letter-spacing: 0.3px;
  font-family: system-ui, -apple-system, "Segoe UI", Tahoma, "Traditional Arabic", "Amiri", Arial, sans-serif;
  user-select: none;
}

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

.header-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #6366f1;
  box-shadow: 0 0 10px rgba(99, 102, 241, 0.6);
}

.brand-title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}

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

.icon-btn {
  background: var(--btn-fn-bg);
  border: 1px solid transparent;
  color: var(--text-secondary);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: var(--btn-fn-hover);
  color: var(--text-primary);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: translateY(1px);
}

/* Theme Toggle Icons */
[data-theme="light"] .icon-sun {
  display: none;
}
[data-theme="light"] .icon-moon {
  display: block;
}

[data-theme="dark"] .icon-sun {
  display: block;
}
[data-theme="dark"] .icon-moon {
  display: none;
}

/* Display Section */
.display-container {
  background: var(--display-bg);
  border: 1px solid var(--display-border);
  border-radius: 18px;
  padding: 16px 20px;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: flex-end;
  text-align: right;
  word-break: break-all;
  overflow-wrap: break-word;
  margin-bottom: 20px;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.04);
  transition: background var(--transition-speed) ease, border-color var(--transition-speed) ease;
}

.previous-display {
  font-size: 0.95rem;
  color: var(--text-secondary);
  min-height: 24px;
  line-height: 1.4;
  margin-bottom: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.current-display {
  font-size: 2.25rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
  letter-spacing: -0.02em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  transition: font-size 0.15s ease;
}

/* Keypad Grid */
.keypad-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.btn {
  font-family: inherit;
  font-size: 1.15rem;
  font-weight: 600;
  border: none;
  outline: none;
  border-radius: 16px;
  min-height: 56px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  user-select: none;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn:focus-visible {
  outline: 2px solid #6366f1;
  outline-offset: 2px;
}

.btn:active, .btn.btn-active {
  transform: scale(0.94);
}

/* Button Variants */
.btn-number {
  background: var(--btn-num-bg);
  color: var(--btn-num-text);
  box-shadow: var(--btn-num-shadow);
}

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

.btn-number:active {
  background: var(--btn-num-active);
}

.btn-action {
  background: var(--btn-act-bg);
  color: var(--btn-act-text);
  font-size: 1.05rem;
}

.btn-action:hover {
  background: var(--btn-act-hover);
}

.btn-function {
  background: var(--btn-fn-bg);
  color: var(--btn-fn-text);
  font-size: 1.05rem;
}

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

.btn-operator {
  background: var(--btn-op-bg);
  color: var(--btn-op-text);
  font-size: 1.35rem;
}

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

.btn-equals {
  grid-row: span 2;
  background: var(--btn-eq-bg);
  color: var(--btn-eq-text);
  font-size: 1.5rem;
  box-shadow: var(--btn-eq-shadow);
}

.btn-equals:hover {
  background: var(--btn-eq-hover);
  filter: brightness(1.05);
}

.btn-zero {
  grid-column: span 2;
  justify-content: flex-start;
  padding-left: 26px;
}

/* History Drawer Panel */
.history-panel {
  position: absolute;
  inset: 0;
  background: var(--panel-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  z-index: 10;
  display: flex;
  flex-direction: column;
  padding: 24px;
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.25s ease;
  border-radius: 28px;
}

.history-panel.is-open {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--panel-border);
  margin-bottom: 16px;
}

.history-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-primary);
}

.close-btn {
  background: transparent;
  border: none;
  font-size: 1.8rem;
  line-height: 1;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: color 0.2s ease;
}

.close-btn:hover {
  color: var(--text-primary);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-right: 4px;
}

.empty-history-text {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.95rem;
  margin: auto;
}

.history-item {
  background: var(--btn-fn-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 14px;
  text-align: right;
  cursor: pointer;
  transition: all 0.2s ease;
}

.history-item:hover {
  background: var(--history-item-hover);
  border-color: #6366f1;
  transform: translateX(-2px);
}

.history-expression {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 2px;
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.history-result {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.history-footer {
  padding-top: 14px;
  border-top: 1px solid var(--panel-border);
  margin-top: 12px;
}

.clear-history-btn {
  width: 100%;
  padding: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  background: var(--btn-act-bg);
  color: var(--btn-act-text);
  cursor: pointer;
  transition: background 0.2s ease;
}

.clear-history-btn:hover {
  background: var(--btn-act-hover);
}

/* Scrollbar Styling */
.history-list {
  scrollbar-width: thin;
  scrollbar-color: var(--text-secondary) transparent;
}

@supports not (scrollbar-color: auto) {
  .history-list::-webkit-scrollbar {
    width: 6px;
  }
  .history-list::-webkit-scrollbar-thumb {
    background: var(--text-secondary);
    border-radius: 4px;
  }
}

/* Responsive Adjustments */
@media (max-width: 400px) {
  body {
    padding: 8px;
  }

  .calculator-card {
    padding: 16px;
    border-radius: 20px;
  }

  .bismillah-title {
    font-size: 1.05rem;
    margin-bottom: 10px;
  }

  .history-panel {
    border-radius: 20px;
    padding: 16px;
  }

  .keypad-grid {
    gap: 8px;
  }

  .btn {
    min-height: 50px;
    font-size: 1.05rem;
    border-radius: 12px;
  }

  .current-display {
    font-size: 1.85rem;
  }
}
