:root {
    --bg: #0f1419;
    --surface: #1a2332;
    --surface2: #243044;
    --border: #2d3a4f;
    --text: #e6edf3;
    --muted: #8b9cb3;
    --accent: #58a6ff;
    --evt-red: #f85149;
    --evt-green: #3fb950;
    --evt-blue: #58a6ff;
    --evt-black: #484f58;
    --font: "JetBrains Mono", "SF Mono", ui-monospace, monospace;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    touch-action: manipulation;
}

body {
    margin: 0;
    min-height: 100%;
    min-height: 100dvh;
    font-family: var(--font);
    font-size: 16px;
    background: var(--bg);
    color: var(--text);
    overflow-x: hidden;
    overscroll-behavior: none;
}

button {
    font-family: inherit;
    font-size: 16px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

.app {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    width: 100%;
    max-width: 420px;
    min-height: 100dvh;
    margin: 0 auto;
    padding:
        calc(0.75rem + var(--safe-top))
        calc(0.75rem + var(--safe-right))
        calc(0.75rem + var(--safe-bottom))
        calc(0.75rem + var(--safe-left));
    justify-content: center;
}

.app-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.5rem;
    flex-shrink: 0;
}

.app-title h1 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--accent);
}

.app-title p {
    margin: 0.15rem 0 0;
    font-size: 0.8rem;
    color: var(--muted);
}

.app-toolbar {
    display: flex;
    gap: 0.35rem;
    flex-shrink: 0;
}

.tool-btn {
    padding: 0.45rem 0.65rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    cursor: pointer;
    min-width: 2.5rem;
    min-height: 2.5rem;
}

.tool-btn.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.tool-btn:disabled {
    opacity: 0.45;
}

.app-main {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.events-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.55rem;
}

.dimension-toggle {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.35rem;
    margin-bottom: 0.55rem;
}

.dim-btn {
    padding: 0.45rem 0.35rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    cursor: pointer;
    font-size: 0.85rem;
}

.dim-btn--split {
    display: flex;
    padding: 0;
    overflow: hidden;
    background: transparent;
}

.dim-part {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    padding: 0.45rem 0.15rem;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1;
}

.dim-part + .dim-part {
    border-left: 1px solid var(--border);
}

.dim-part--red {
    background: rgba(248, 81, 73, 0.15);
    color: var(--evt-red);
}

.dim-part--black {
    background: rgba(22, 27, 34, 0.95);
    color: #c9d1d9;
}

.dim-part--blue {
    background: rgba(88, 166, 255, 0.15);
    color: var(--evt-blue);
}

.dim-btn.is-active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.dim-btn--split.is-active {
    background: transparent;
    color: inherit;
    box-shadow: 0 0 0 2px var(--accent);
}

.events-strip {
    display: grid;
    grid-template-columns: repeat(13, minmax(0, 1fr));
    gap: 2px;
    min-height: 1.85rem;
    align-items: stretch;
}

.events-empty {
    grid-column: 1 / -1;
    text-align: center;
    color: var(--muted);
    font-size: 0.85rem;
    line-height: 1.85rem;
}

.evt-chip {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    padding: 0.1rem 0;
    border-radius: 4px;
    font-size: clamp(0.55rem, 2.8vw, 0.72rem);
    font-weight: 700;
    line-height: 1;
    aspect-ratio: 1;
}

.evt-green {
    background: rgba(63, 185, 80, 0.18);
    color: var(--evt-green);
    border: 1px solid rgba(63, 185, 80, 0.55);
}

.evt-red {
    background: rgba(248, 81, 73, 0.15);
    color: var(--evt-red);
    border: 1px solid rgba(248, 81, 73, 0.45);
}

.evt-black {
    background: rgba(22, 27, 34, 0.95);
    color: #c9d1d9;
    border: 1px solid rgba(72, 79, 88, 0.9);
}

.evt-blue {
    background: rgba(88, 166, 255, 0.15);
    color: var(--evt-blue);
    border: 1px solid rgba(88, 166, 255, 0.45);
}

.spin-preview {
    margin-top: 0.45rem;
    text-align: center;
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--accent);
    min-height: 1.6rem;
}

.numpad-section {
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.numpad {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
}

.numpad-key {
    padding: 0;
    min-height: 3.1rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface2);
    color: var(--text);
    font-size: 1.25rem;
    font-weight: 600;
    cursor: pointer;
}

.numpad-key:active:not(:disabled) {
    background: var(--border);
}

.numpad-key:disabled {
    opacity: 0.45;
}

.submit-btn {
    width: 100%;
    min-height: 3rem;
    border-radius: 10px;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #fff;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
}

.submit-btn:disabled {
    opacity: 0.45;
}

.history-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
}

.history-btn {
    min-height: 2.75rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
}

.history-btn:disabled {
    opacity: 0.45;
}

@media (max-height: 700px) {
    .app {
        justify-content: flex-start;
    }

    .numpad-key {
        min-height: 2.75rem;
    }
}

@media (min-width: 420px) {
    .evt-chip {
        font-size: 0.72rem;
    }
}

html.is-immersive {
    background: var(--bg);
}

html.is-immersive body {
    overflow: hidden;
}

html.is-immersive .app {
    min-height: 100dvh;
    max-width: none;
    width: 100%;
    justify-content: flex-start;
    padding-top: calc(0.35rem + var(--safe-top));
}

html.is-immersive .app-header {
    margin-bottom: 0.35rem;
}
