/**
 * Calculator App Styles
 *
 * A production-ready calculator with accessible, responsive design
 */

/* ========================================
   App Page Layout
   ======================================== */

.app-page {
    padding: var(--space-4);
    padding-top: calc(var(--header-height) + var(--space-4));
    min-height: calc(100vh - var(--header-height));
    display: flex;
    flex-direction: column;
    align-items: center;
}

.app-container {
    width: 100%;
    max-width: 400px;
    margin: 0 auto;
}

.app-header {
    text-align: center;
    margin-bottom: var(--space-6);
}

.app-header h1 {
    font-size: var(--font-size-2xl);
    margin-bottom: var(--space-2);
}

.app-description {
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
    margin-bottom: 0;
}

/* ========================================
   Calculator
   ======================================== */

.calculator {
    background: var(--color-bg-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-lg);

    /* Material Design color variables */
    --calc-btn-number: var(--color-bg-card);
    --calc-btn-number-hover: var(--color-bg-alt);
    --calc-btn-function: var(--color-bg-alt);
    --calc-btn-function-text: var(--color-text);
    --calc-btn-operator: var(--color-primary);
    --calc-btn-operator-hover: var(--color-primary-dark);
    --calc-btn-equals: #4caf50;
    --calc-btn-equals-hover: #43a047;
    --calc-btn-scientific: var(--color-bg-alt);
    --calc-btn-scientific-text: var(--color-primary);
    --ripple-color: rgba(255, 255, 255, 0.3);
}

/* Display */
.calc-display {
    position: relative;
    padding: var(--space-6);
    background: var(--color-bg-alt);
    border-bottom: 1px solid var(--color-border);
    min-height: 120px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;
    cursor: grab;
    touch-action: pan-y;
    user-select: none;
}

.calc-display.dragging {
    cursor: grabbing;
}

/* History drawer hint (small handle) */
.history-drawer-hint {
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0.4;
}

.history-drawer-hint .hint-line {
    display: block;
    width: 32px;
    height: 4px;
    background: var(--color-border-dark, var(--color-border));
    border-radius: 2px;
}

.calc-expression {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    min-height: 1.5em;
    word-break: break-all;
    text-align: right;
    width: 100%;
}

.calc-result {
    font-size: 2.5rem;
    font-weight: 600;
    font-family: var(--font-mono);
    line-height: 1.2;
    word-break: break-all;
    text-align: right;
    width: 100%;
    color: var(--color-text);
}

.calc-result.error {
    color: var(--color-error);
    font-size: var(--font-size-lg);
}

/* ========================================
   History Drawer (Swipe Down)
   ======================================== */

.history-drawer {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: 0;
    overflow: hidden;
    background: var(--color-bg-card);
    border-top: 1px solid var(--color-border);
    box-shadow: var(--shadow-lg);
    transition: max-height 0.3s ease-in-out;
    z-index: 10;
}

.history-drawer[hidden] {
    display: block;
}

.history-drawer.open {
    max-height: 300px;
    overflow-y: auto;
}

.history-drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg-alt);
    position: sticky;
    top: 0;
}

.history-drawer-header h2 {
    font-size: var(--font-size-sm);
    font-weight: 600;
    margin: 0;
}

/* ========================================
   Scientific Toggle Button
   ======================================== */

.calc-sci-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: var(--space-2);
    background: var(--color-bg-alt);
    border: none;
    border-bottom: 1px solid var(--color-border);
    cursor: pointer;
    transition: background var(--transition-fast);
    color: var(--color-text-muted);
}

.calc-sci-toggle:hover {
    background: var(--color-border);
}

.calc-sci-toggle:focus-visible {
    outline: none;
    box-shadow: inset var(--focus-ring);
}

.calc-sci-toggle .toggle-arrow {
    font-size: var(--font-size-xs);
    transition: transform var(--transition-base);
}

.calc-sci-toggle.active .toggle-arrow,
.calc-sci-toggle[aria-expanded="true"] .toggle-arrow {
    transform: rotate(180deg);
}

/* ========================================
   Scientific Functions Panel
   ======================================== */

.calc-scientific {
    display: none;
    flex-direction: column;
    gap: 1px;
    background: var(--color-border);
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-in-out;
}

.calc-scientific:not([hidden]) {
    display: flex;
    max-height: 200px;
}

.calc-scientific-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 1px;
}

.calc-btn-scientific {
    background: var(--calc-btn-scientific);
    color: var(--calc-btn-scientific-text);
    font-size: var(--font-size-sm);
    min-height: 48px;
    font-weight: 500;
}

.calc-btn-scientific:hover {
    background: var(--color-border);
}

.calc-btn-scientific.active {
    background: var(--color-primary);
    color: white;
}

.calc-btn-scientific sup {
    font-size: 0.7em;
    vertical-align: super;
}

.angle-mode {
    font-weight: 600;
    font-size: var(--font-size-xs);
}

/* Keypad */
.calc-keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1px;
    background: var(--color-border);
    padding: 1px;
}

/* Buttons */
.calc-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-5);
    font-size: var(--font-size-xl);
    font-weight: 500;
    background: var(--calc-btn-number);
    border: none;
    cursor: pointer;
    transition: background var(--transition-fast), transform 0.1s ease;
    min-height: 72px;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overflow: hidden;
}

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

.calc-btn:active {
    background: var(--color-border);
    transform: scale(0.95);
}

.calc-btn:focus-visible {
    outline: none;
    box-shadow: inset var(--focus-ring);
    z-index: 1;
    position: relative;
}

/* Ripple effect */
.calc-btn::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: var(--ripple-color);
    transform: translate(-50%, -50%);
    opacity: 0;
    pointer-events: none;
}

.calc-btn.ripple::after {
    animation: ripple-effect 0.6s ease-out;
}

@keyframes ripple-effect {
    0% {
        width: 0;
        height: 0;
        opacity: 0.5;
    }
    100% {
        width: 200%;
        height: 200%;
        opacity: 0;
    }
}

/* Number buttons - larger font */
.calc-btn-number {
    background: var(--calc-btn-number);
    font-size: var(--font-size-2xl);
    font-weight: 400;
}

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

/* Function buttons (AC, (, )) */
.calc-btn-function {
    background: var(--calc-btn-function);
    color: var(--calc-btn-function-text);
}

.calc-btn-function:hover {
    background: var(--color-border);
}

/* Operator buttons */
.calc-btn-operator {
    background: var(--calc-btn-operator);
    color: white;
    --ripple-color: rgba(255, 255, 255, 0.4);
}

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

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

.calc-btn-operator.active {
    background: var(--color-primary-light, #6366f1);
    box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.3);
}

/* Equals button - distinct green color */
.calc-btn-equals {
    background: var(--calc-btn-equals);
    color: white;
    --ripple-color: rgba(255, 255, 255, 0.4);
}

.calc-btn-equals:hover {
    background: var(--calc-btn-equals-hover);
}

/* ========================================
   History Styles (used in drawer)
   ======================================== */

.btn-text {
    background: none;
    border: none;
    color: var(--color-primary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
}

.btn-text:hover {
    background: rgba(79, 70, 229, 0.1);
}

.btn-text:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.history-list {
    max-height: 200px;
    overflow-y: auto;
    padding: 0;
    margin: 0;
    list-style: none;
}

.history-item {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.history-item:last-child {
    border-bottom: none;
}

.history-item:hover {
    background: var(--color-bg-alt);
}

.history-item:focus-visible {
    outline: none;
    box-shadow: inset var(--focus-ring);
}

.history-expression {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    font-family: var(--font-mono);
}

.history-result {
    font-size: var(--font-size-base);
    font-weight: 600;
    font-family: var(--font-mono);
    color: var(--color-text);
}

.history-empty {
    padding: var(--space-6);
    text-align: center;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

/* ========================================
   Help Panel (Modal)
   ======================================== */

.help-panel {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

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

.help-content {
    background: var(--color-bg-card);
    border-radius: var(--radius-lg);
    max-width: 500px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-xl);
}

.help-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4) var(--space-6);
    border-bottom: 1px solid var(--color-border);
    position: sticky;
    top: 0;
    background: var(--color-bg-card);
}

.help-header h2 {
    font-size: var(--font-size-lg);
    margin: 0;
}

.help-close {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-xl);
    border-radius: var(--radius-md);
    color: var(--color-text-muted);
}

.help-close:hover {
    background: var(--color-bg-alt);
    color: var(--color-text);
}

.help-close:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.help-body {
    padding: var(--space-6);
}

.help-section {
    margin-bottom: var(--space-6);
}

.help-section:last-child {
    margin-bottom: 0;
}

.help-section h3 {
    font-size: var(--font-size-base);
    margin-bottom: var(--space-4);
    color: var(--color-text);
}

.help-shortcuts {
    margin: 0;
}

.shortcut-item {
    display: flex;
    gap: var(--space-4);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--color-border);
}

.shortcut-item:last-child {
    border-bottom: none;
}

.shortcut-item dt {
    flex-shrink: 0;
    min-width: 120px;
}

.shortcut-item dd {
    margin: 0;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

kbd {
    display: inline-block;
    padding: var(--space-1) var(--space-2);
    font-family: var(--font-mono);
    font-size: var(--font-size-xs);
    background: var(--color-bg-alt);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: 0 1px 0 var(--color-border);
}

.help-features {
    margin: 0;
    padding-left: var(--space-5);
}

.help-features li {
    margin-bottom: var(--space-2);
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

/* ========================================
   Header Actions
   ======================================== */

.nav-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.btn-icon {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-full);
    font-size: var(--font-size-lg);
    font-weight: 600;
    color: var(--color-text-muted);
    background: var(--color-bg-alt);
    border: 1px solid var(--color-border);
}

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

.btn-icon:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

/* ========================================
   Responsive Design
   ======================================== */

@media (max-width: 480px) {
    .app-page {
        padding: var(--space-2);
        padding-top: calc(var(--header-height) + var(--space-2));
    }

    .app-header {
        margin-bottom: var(--space-4);
    }

    .calc-display {
        padding: var(--space-4);
        min-height: 100px;
    }

    .calc-result {
        font-size: 2rem;
    }

    .calc-btn {
        padding: var(--space-4);
        min-height: 64px;
        font-size: var(--font-size-lg);
    }

    .calc-btn-number {
        font-size: var(--font-size-xl);
    }

    .calc-scientific-row {
        grid-template-columns: repeat(5, 1fr);
    }

    .calc-btn-scientific {
        font-size: var(--font-size-xs);
        min-height: 44px;
        padding: var(--space-2);
    }

    .shortcut-item {
        flex-direction: column;
        gap: var(--space-1);
    }

    .shortcut-item dt {
        min-width: auto;
    }
}

/* ========================================
   Reduced Motion
   ======================================== */

@media (prefers-reduced-motion: reduce) {
    .calc-btn,
    .history-item,
    .btn-text,
    .btn-icon,
    .calc-sci-toggle,
    .calc-scientific,
    .history-drawer,
    .toggle-arrow {
        transition: none;
    }

    .calc-btn::after {
        animation: none;
    }

    .calc-btn:active {
        transform: none;
    }
}

/* ========================================
   Print Styles
   ======================================== */

@media print {
    .calc-keypad,
    .calc-scientific,
    .calc-sci-toggle,
    .history-drawer,
    .history-drawer-hint,
    .help-panel,
    .nav-actions {
        display: none;
    }

    .calculator {
        box-shadow: none;
        border: 1px solid #ccc;
    }
}
