#!/usr/bin/env python3
# IPSONIC-ADCTOP — Nginx access-log monitoring utility
# Website: https://ipsonic.com/adctop
# Contact: ipsonic@mailbox.org
#
# DISCLAIMER:
# This software is provided "as is", without warranty of any kind.
# It is intended as an operational visibility and troubleshooting aid,
# not as a substitute for security controls or comprehensive monitoring.
# Review its behavior and permissions before using it in production.
# The operator is responsible for appropriate handling of access-log data.
#
"""Interactive nginx access-log monitor (standard library only)."""
import argparse
import curses
import glob
import os
import re
import select
import sys
import shlex
import time
import termios
import tty
from collections import Counter, defaultdict, deque
from datetime import datetime
from urllib.parse import urlsplit

VERSION = '0.3.4'

CLI_DISCLAIMER = (
    'Website: https://ipsonic.com/adctop\n'
    'Contact: ipsonic@mailbox.org\n'
    '\n'
    'DISCLAIMER:\n'
    'This software is provided "as is", without warranty of any kind.\n'
    'It is intended as an operational visibility and troubleshooting aid,\n'
    'not as a substitute for security controls or comprehensive monitoring.\n'
    'Review its behavior and permissions before using it in production.\n'
    'The operator is responsible for appropriate handling of access-log data.'
)

# Ten semantic color pairs shared by all screens. Each theme supplies a
# 256-color palette and an ANSI 8-color fallback.
THEMES = {
    'meow': ('IPSONIC ADC / SecOps: navy, mint, cyan, violet and magenta (default)',
             [(231, 17), (81, None), (48, None), (221, None), (210, None),
              (231, 24), (231, 89), (17, 48), (141, None), (17, 81)],
             [(7, 4), (6, None), (2, None), (3, None), (1, None),
              (7, 4), (7, 5), (4, 2), (5, None), (4, 6)]),
    'turbo': ('Turbo Vision / DOS blue',
              [(231, 19), (117, None), (120, None), (229, None), (210, None),
               (231, 25), (231, 124), (16, 120), (159, None), (16, 159)],
              [(7, 4), (6, None), (2, None), (3, None), (1, None),
               (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'amber': ('Amber monochrome / classic terminal',
              [(232, 220), (178, None), (220, None), (214, None), (166, None),
               (232, 214), (231, 130), (232, 220), (214, None), (232, 220)],
              [(0, 3), (3, None), (3, None), (3, None), (1, None),
               (0, 3), (7, 1), (0, 3), (3, None), (0, 3)]),
    'matrix': ('Green phosphor / old-school CRT',
               [(232, 46), (34, None), (46, None), (118, None), (160, None),
                (232, 40), (231, 160), (232, 46), (82, None), (232, 82)],
               [(0, 2), (2, None), (2, None), (3, None), (1, None),
                (0, 2), (7, 1), (0, 2), (2, None), (0, 2)]),
    'daylight': ('High contrast / bright terminal background',
                 [(231, 18), (18, None), (22, None), (94, None), (124, None),
                  (231, 24), (231, 124), (231, 28), (54, None), (231, 18)],
                 [(7, 4), (4, None), (2, None), (3, None), (1, None),
                  (7, 4), (7, 1), (7, 2), (5, None), (7, 4)]),
    'sgi': ('SGI Indigo / violet and cyan',
            [(231, 60), (159, None), (121, None), (228, None), (210, None),
             (231, 25), (231, 124), (16, 121), (183, None), (16, 159)],
            [(7, 5), (6, None), (2, None), (3, None), (1, None),
             (7, 4), (7, 1), (0, 2), (5, None), (0, 6)]),
    'irix': ('IRIX 4Dwm / cool grey and teal',
             [(16, 153), (24, None), (22, None), (94, None), (124, None),
              (231, 24), (231, 124), (231, 29), (54, None), (231, 24)],
             [(0, 6), (4, None), (2, None), (3, None), (1, None),
              (7, 4), (7, 1), (7, 2), (5, None), (7, 4)]),
    'amiga': ('Amiga Workbench / blue, orange and white',
              [(231, 25), (159, None), (120, None), (220, None), (215, None),
               (231, 31), (231, 124), (16, 220), (117, None), (16, 215)],
              [(7, 4), (6, None), (2, None), (3, None), (3, None),
               (7, 6), (7, 1), (0, 3), (6, None), (0, 3)]),
    'workbench': ('Amiga Workbench light / grey and navy',
                  [(231, 24), (18, None), (22, None), (94, None), (124, None),
                   (231, 25), (231, 124), (231, 28), (54, None), (231, 24)],
                  [(7, 4), (4, None), (2, None), (3, None), (1, None),
                   (7, 4), (7, 1), (7, 2), (5, None), (7, 4)]),
    'norton': ('Norton Commander / navy and cyan',
               [(231, 20), (159, None), (120, None), (229, None), (210, None),
                (231, 27), (231, 124), (16, 159), (117, None), (16, 159)],
               [(7, 4), (6, None), (2, None), (3, None), (1, None),
                (7, 4), (7, 1), (0, 6), (6, None), (0, 6)]),
    'solar': ('Solaris CDE / sand, navy and teal',
              [(16, 223), (18, None), (22, None), (94, None), (124, None),
               (231, 24), (231, 124), (231, 29), (54, None), (231, 24)],
              [(0, 3), (4, None), (2, None), (3, None), (1, None),
               (7, 4), (7, 1), (7, 2), (5, None), (7, 4)]),

    'wallboard': ('Wallboard / large-screen high contrast',
        [(231, 17), (51, None), (118, None), (226, None), (203, None), (231, 25), (231, 124), (16, 118), (159, None), (16, 51)],
        [(7, 4), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'snow': ('Snow / bright high contrast',
        [(17, 231), (18, None), (22, None), (94, None), (124, None), (231, 25), (231, 124), (22, 22), (54, None), (17, 7)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (5, None), (0, 6)]),
    'graphite': ('Graphite / neutral dark dashboard',
        [(231, 235), (117, None), (120, None), (229, None), (210, None), (231, 239), (231, 124), (16, 120), (159, None), (16, 117)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'fireplace': ('Fireplace / warm ember glow',
        [(230, 52), (215, None), (220, None), (208, None), (203, None), (231, 94), (231, 124), (16, 220), (223, None), (16, 215)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'campfire': ('Campfire / midnight orange and gold',
        [(230, 234), (208, None), (178, None), (220, None), (196, None), (231, 94), (231, 124), (16, 178), (215, None), (16, 220)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'ocean': ('Ocean / deep blue and turquoise',
        [(231, 17), (87, None), (121, None), (229, None), (203, None), (231, 24), (231, 124), (16, 121), (159, None), (16, 87)],
        [(7, 4), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'arctic': ('Arctic / ice blue light dashboard',
        [(17, 195), (24, None), (22, None), (94, None), (124, None), (231, 31), (231, 124), (22, 22), (54, None), (17, 24)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (5, None), (0, 6)]),
    'sunset': ('Sunset / plum, coral and gold',
        [(231, 53), (219, None), (215, None), (229, None), (203, None), (231, 90), (231, 124), (16, 215), (222, None), (16, 219)],
        [(7, 5), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'terminal': ('Terminal / monochrome high contrast',
        [(231, 16), (255, None), (231, None), (250, None), (203, None), (231, 238), (231, 124), (16, 231), (231, None), (16, 231)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'paper': ('Paper / soft cream and ink',
        [(16, 230), (18, None), (22, None), (94, None), (124, None), (231, 24), (231, 124), (22, 22), (54, None), (16, 18)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (5, None), (0, 6)]),
    'copper': ('Copper / industrial orange and teal',
        [(231, 236), (215, None), (121, None), (229, None), (203, None), (231, 94), (231, 124), (16, 121), (159, None), (16, 215)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'midnight': ('Midnight / dark blue and silver',
        [(255, 234), (153, None), (121, None), (229, None), (203, None), (231, 24), (231, 124), (16, 121), (153, None), (16, 153)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 1), (0, 2), (6, None), (0, 6)]),
    'emerald': ('Emerald / vivid green on deep forest',
        [(231, 22), (120, None), (157, None), (229, None), (210, None), (231, 28), (231, 124), (16, 120), (159, None), (16, 157)],
        [(7, 2), (2, None), (2, None), (3, None), (1, None), (7, 2), (7, 1), (0, 2), (6, None), (0, 2)]),
    'cobalt': ('Cobalt / bright blue with white and yellow',
        [(231, 18), (159, None), (231, None), (229, None), (210, None), (231, 26), (231, 124), (16, 159), (117, None), (16, 229)],
        [(7, 4), (6, None), (7, None), (3, None), (1, None), (7, 4), (7, 1), (0, 6), (6, None), (0, 3)]),
    'crimson': ('Crimson / deep red with warm white',
        [(231, 52), (224, None), (229, None), (220, None), (203, None), (231, 88), (231, 124), (16, 229), (217, None), (16, 224)],
        [(7, 1), (7, None), (3, None), (3, None), (1, None), (7, 1), (7, 1), (0, 3), (7, None), (0, 7)]),
    'lime': ('Lime / bright green and charcoal',
        [(231, 232), (154, None), (190, None), (229, None), (203, None), (16, 154), (231, 124), (16, 190), (118, None), (16, 154)],
        [(7, 0), (2, None), (2, None), (3, None), (1, None), (0, 2), (7, 1), (0, 2), (2, None), (0, 2)]),
    'azure': ('Azure / blue and icy cyan on midnight',
        [(231, 17), (117, None), (159, None), (229, None), (210, None), (231, 25), (231, 124), (16, 159), (153, None), (16, 117)],
        [(7, 4), (6, None), (6, None), (3, None), (1, None), (7, 4), (7, 1), (0, 6), (6, None), (0, 6)]),
    'ruby': ('Ruby / scarlet and rose on black',
        [(231, 16), (217, None), (224, None), (229, None), (203, None), (231, 88), (231, 124), (16, 224), (210, None), (16, 217)],
        [(7, 0), (1, None), (7, None), (3, None), (1, None), (7, 1), (7, 1), (0, 7), (1, None), (0, 7)]),
    'sapphire': ('Sapphire / saturated blue on pale ice',
        [(17, 195), (18, None), (24, None), (94, None), (124, None), (231, 25), (231, 124), (231, 24), (54, None), (231, 18)],
        [(0, 7), (4, None), (4, None), (3, None), (1, None), (7, 4), (7, 1), (7, 4), (5, None), (7, 4)]),
    'forest': ('Forest / dark green and cream',
        [(230, 22), (151, None), (186, None), (222, None), (210, None), (231, 28), (231, 124), (16, 186), (157, None), (16, 151)],
        [(7, 2), (2, None), (3, None), (3, None), (1, None), (7, 2), (7, 1), (0, 3), (2, None), (0, 2)]),
    'signal': ('Signal / high-alert red and white',
        [(231, 16), (255, None), (120, None), (226, None), (203, None), (231, 160), (231, 124), (16, 226), (231, None), (16, 231)],
        [(7, 0), (7, None), (2, None), (3, None), (1, None), (7, 1), (7, 1), (0, 3), (7, None), (0, 7)]),
    'teletext': ('Teletext / broadcast primary colors',
        [(231, 16), (51, None), (46, None), (226, None), (196, None), (231, 19), (231, 27), (16, 46), (51, None), (16, 51)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'ceefax': ('Ceefax / BBC videotext blue',
        [(231, 19), (51, None), (118, None), (226, None), (203, None), (231, 21), (231, 27), (16, 118), (51, None), (16, 51)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'prestel': ('Prestel / videotex cyan and yellow',
        [(231, 17), (51, None), (118, None), (226, None), (196, None), (231, 25), (231, 90), (16, 118), (51, None), (16, 51)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'minitel': ('Minitel / French videotex monochrome',
        [(231, 16), (255, None), (252, None), (250, None), (231, None), (231, 238), (231, 244), (16, 252), (255, None), (16, 255)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'ansi_bbs': ('ANSI BBS / neon terminal art',
        [(231, 16), (201, None), (51, None), (226, None), (196, None), (231, 55), (231, 27), (16, 51), (201, None), (16, 201)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'bbs_ice': ('BBS ice colors / bright blue',
        [(231, 19), (159, None), (231, None), (226, None), (203, None), (231, 27), (231, 57), (16, 231), (159, None), (16, 159)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'bbs_fire': ('BBS fire / red orange and gold',
        [(231, 52), (215, None), (220, None), (226, None), (203, None), (231, 88), (231, 130), (16, 220), (215, None), (16, 215)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'dos_cga': ('DOS CGA / magenta and cyan',
        [(231, 16), (201, None), (51, None), (226, None), (196, None), (231, 90), (231, 27), (16, 51), (201, None), (16, 201)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'dos_ega': ('DOS EGA / bright primary palette',
        [(231, 17), (51, None), (118, None), (226, None), (196, None), (231, 20), (231, 90), (16, 118), (51, None), (16, 51)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'ibm_mda': ('IBM MDA / white phosphor',
        [(255, 16), (252, None), (231, None), (250, None), (255, None), (255, 238), (255, 245), (16, 231), (252, None), (16, 252)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'hercules': ('Hercules / green monochrome',
        [(120, 16), (46, None), (118, None), (82, None), (40, None), (120, 22), (120, 28), (16, 118), (46, None), (16, 46)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'vt100': ('VT100 / soft monochrome',
        [(252, 234), (250, None), (255, None), (245, None), (231, None), (252, 238), (252, 244), (16, 255), (250, None), (16, 250)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'vt220': ('VT220 / green phosphor',
        [(157, 232), (120, None), (190, None), (154, None), (118, None), (157, 22), (157, 28), (16, 190), (120, None), (16, 120)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'apple_ii': ('Apple II / amber and green',
        [(229, 16), (220, None), (118, None), (214, None), (203, None), (229, 94), (229, 28), (16, 118), (220, None), (16, 220)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'commodore64': ('Commodore 64 / blue on blue',
        [(231, 19), (153, None), (159, None), (229, None), (210, None), (231, 25), (231, 60), (16, 159), (153, None), (16, 153)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'zx_spectrum': ('ZX Spectrum / vivid color clash',
        [(231, 16), (226, None), (46, None), (201, None), (196, None), (231, 19), (231, 90), (16, 46), (226, None), (16, 226)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'atari_st': ('Atari ST / desktop grey',
        [(16, 252), (18, None), (22, None), (94, None), (124, None), (16, 250), (16, 153), (231, 22), (18, None), (231, 18)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (5, None), (0, 6)]),
    'mac_classic': ('Classic Macintosh / monochrome',
        [(16, 231), (238, None), (16, None), (240, None), (16, None), (16, 250), (16, 255), (16, 16), (238, None), (16, 238)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (5, None), (0, 6)]),
    'mono_black': ('Monochrome black / pure white',
        [(231, 16), (255, None), (231, None), (250, None), (231, None), (231, 238), (231, 255), (16, 231), (255, None), (16, 255)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'mono_white': ('Monochrome white / pure black',
        [(16, 231), (238, None), (16, None), (240, None), (16, None), (16, 250), (16, 255), (16, 16), (238, None), (16, 238)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (5, None), (0, 6)]),
    'phosphor_blue': ('Blue phosphor / bright cyan',
        [(159, 16), (51, None), (117, None), (153, None), (87, None), (159, 24), (159, 31), (16, 117), (51, None), (16, 51)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'phosphor_red': ('Red phosphor / crimson',
        [(224, 16), (210, None), (217, None), (203, None), (196, None), (224, 52), (224, 88), (16, 217), (210, None), (16, 210)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'noc_red': ('NOC alert / red wall display',
        [(231, 16), (203, None), (120, None), (226, None), (196, None), (231, 88), (231, 160), (16, 120), (203, None), (16, 203)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'noc_green': ('NOC healthy / green wall display',
        [(231, 16), (118, None), (46, None), (226, None), (203, None), (231, 22), (231, 28), (16, 46), (118, None), (16, 118)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'noc_blue': ('NOC overview / blue wall display',
        [(231, 17), (159, None), (120, None), (226, None), (203, None), (231, 19), (231, 25), (16, 120), (159, None), (16, 159)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'high_vis': ('High visibility / yellow on black',
        [(231, 16), (226, None), (231, None), (220, None), (196, None), (231, 238), (231, 226), (16, 231), (226, None), (16, 226)],
        [(7, 0), (6, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (6, None), (0, 6)]),
    'sepia': ('Sepia / warm paper terminal',
        [(52, 230), (94, None), (58, None), (130, None), (124, None), (52, 223), (52, 180), (16, 58), (94, None), (16, 94)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (5, None), (0, 6)]),
    'lavender': ('Lavender / light purple',
        [(53, 225), (55, None), (22, None), (94, None), (124, None), (53, 183), (53, 189), (16, 22), (55, None), (16, 55)],
        [(0, 7), (4, None), (2, None), (3, None), (1, None), (7, 4), (7, 5), (0, 2), (5, None), (0, 6)]),
}
THEME_NAMES = tuple(THEMES)

# Backgrounds are explicit: relying on the terminal default leaves black gaps
# on light themes and makes palette switching inconsistent.
THEME_BACKGROUNDS = {
    'meow': (17, 4),
    'turbo': (19, 4),
    'amber': (232, 0),
    'matrix': (232, 0),
    'daylight': (255, 7),
    'sgi': (60, 5),
    'irix': (254, 7),
    'amiga': (25, 4),
    'workbench': (252, 7),
    'norton': (20, 4),
    'solar': (230, 7),
    'wallboard': (17, 4),
    'snow': (231, 7),
    'graphite': (235, 0),
    'fireplace': (52, 0),
    'campfire': (234, 0),
    'ocean': (17, 4),
    'arctic': (195, 7),
    'sunset': (53, 5),
    'terminal': (16, 0),
    'paper': (230, 7),
    'copper': (236, 0),
    'midnight': (234, 0),
    'emerald': (22, 2),
    'cobalt': (18, 4),
    'crimson': (52, 1),
    'lime': (232, 0),
    'azure': (17, 4),
    'ruby': (16, 0),
    'sapphire': (195, 7),
    'forest': (22, 2),
    'signal': (16, 0),
    'teletext': (16, 0),
    'ceefax': (19, 0),
    'prestel': (17, 0),
    'minitel': (16, 0),
    'ansi_bbs': (16, 0),
    'bbs_ice': (19, 0),
    'bbs_fire': (52, 0),
    'dos_cga': (16, 0),
    'dos_ega': (17, 0),
    'ibm_mda': (16, 0),
    'hercules': (16, 0),
    'vt100': (234, 0),
    'vt220': (232, 0),
    'apple_ii': (16, 0),
    'commodore64': (19, 0),
    'zx_spectrum': (16, 0),
    'atari_st': (252, 7),
    'mac_classic': (231, 7),
    'mono_black': (16, 0),
    'mono_white': (231, 7),
    'phosphor_blue': (16, 0),
    'phosphor_red': (16, 0),
    'noc_red': (16, 0),
    'noc_green': (16, 0),
    'noc_blue': (17, 0),
    'high_vis': (16, 0),
    'sepia': (230, 7),
    'lavender': (225, 7),
}

class TerminalKeys:
    """Read keyboard bytes exclusively from the controlling terminal."""

    ESCAPES = {b'[A': curses.KEY_UP, b'[B': curses.KEY_DOWN,
               b'[5~': curses.KEY_PPAGE, b'[6~': curses.KEY_NPAGE,
               b'OA': curses.KEY_UP, b'OB': curses.KEY_DOWN}

    def __init__(self):
        self.fd = os.open('/dev/tty', os.O_RDONLY | os.O_NONBLOCK)
        self.original = termios.tcgetattr(self.fd)
        tty.setcbreak(self.fd)
        self.pending = bytearray()
        self.escape_since = None

    def close(self):
        termios.tcsetattr(self.fd, termios.TCSADRAIN, self.original)
        os.close(self.fd)

    def get(self):
        while select.select([self.fd], [], [], 0)[0]:
            try:
                chunk = os.read(self.fd, 4096)
            except BlockingIOError:
                break
            if not chunk:
                break
            self.pending.extend(chunk)
        if not self.pending:
            return -1
        if self.pending[0] != 27:
            return self.pending.pop(0)
        if len(self.pending) == 1:
            if self.escape_since is None:
                self.escape_since = time.monotonic()
            if time.monotonic() - self.escape_since < 0.05:
                return -1
            self.escape_since = None
            return self.pending.pop(0)
        self.escape_since = None
        for seq, code in self.ESCAPES.items():
            if self.pending[1:].startswith(seq):
                del self.pending[:len(seq) + 1]
                return code
        if any(seq.startswith(bytes(self.pending[1:])) for seq in self.ESCAPES):
            return -1
        return self.pending.pop(0)


LOG_RE = re.compile(
    r'^(?P<ip>\S+) \S+ \S+ \[(?P<date>[^]]+)\] '
    r'"(?P<request>[^"]*)" (?P<status>\d{3}) (?P<bytes>\d+|-) '
    r'"(?P<referer>[^"]*)" "(?P<agent>[^"]*)"(?P<extra>.*)$'
)


def human(n):
    n = float(n)
    for unit in ('B', 'KiB', 'MiB', 'GiB', 'TiB'):
        if abs(n) < 1024 or unit == 'TiB':
            return f'{n:.1f}{unit}'
        n /= 1024


def parse(line, now, referer_domain=False):
    m = LOG_RE.match(line)
    if not m:
        return None
    d = m.groupdict()
    request = d['request'].split()
    method = request[0] if request else '-'
    url = request[1] if len(request) > 1 else '-'
    ref = d['referer'] or '-'
    if referer_domain and ref != '-':
        try:
            ref = urlsplit(ref).hostname or ref
        except ValueError:
            pass
    extra = d['extra'].strip()
    duration = None
    request_bytes = None
    # Supported suffixes (after the combined format):
    #   0.012 456                  (request_time, request_length)
    #   "0.012" "456"              (quoted fields)
    #   request_time=0.012 request_length=456
    #   rt=0.012 rl=456            (common nginx custom format)
    #   - 456                      (missing request_time)
    # Values are optional; a missing value is never treated as zero.
    if extra:
        tokens = [token.strip("\"'") for token in extra.split()]
        positional = []
        named = {}
        for token in tokens:
            if '=' in token:
                key, value = token.split('=', 1)
                named[key.lower().lstrip('$')] = value.strip(chr(34) + chr(39))
            else:
                positional.append(token)

        def numeric(value, cast):
            if value is None or value in ('', '-'):
                return None
            try:
                result = cast(value)
                return result if result >= 0 else None
            except (ValueError, OverflowError):
                return None

        time_value = named.get('request_time', named.get('rt'))
        length_value = named.get('request_length', named.get('rl'))
        if time_value is None and positional:
            time_value = positional[0]
        if length_value is None and len(positional) > 1:
            length_value = positional[1]
        duration = numeric(time_value, float)
        request_bytes = numeric(length_value, int)
    return dict(t=now, ip=d['ip'], method=method, url=url,
                status=int(d['status']), bytes=int(d['bytes']) if d['bytes'] != '-' else 0,
                referer=ref, agent=d['agent'] or '-', duration=duration, request_bytes=request_bytes)


class Tail:
    def __init__(self, path, from_start=False):
        self.path = path
        self.from_start = from_start
        self.fd = None
        self.identity = None
        self.pending = ''
        self.open(initial=True)

    def open(self, initial=False):
        if self.fd:
            self.fd.close()
        self.fd = open(self.path, 'r', encoding='utf-8', errors='replace')
        st = os.fstat(self.fd.fileno())
        self.identity = (st.st_dev, st.st_ino)
        self.pending = ''
        if initial and not self.from_start:
            self.fd.seek(0, os.SEEK_END)

    def read(self):
        try:
            st = os.stat(self.path)
            if (st.st_dev, st.st_ino) != self.identity:
                self.open()
            elif st.st_size < self.fd.tell():
                self.fd.seek(0)
                self.pending = ''
        except FileNotFoundError:
            # Keep reading old inode until replacement appears.
            pass
        lines = []
        # Never let a busy logfile starve terminal keyboard processing.
        for _ in range(2048):
            chunk = self.fd.readline()
            if not chunk:
                break
            self.pending += chunk
            if self.pending.endswith('\n'):
                lines.append(self.pending)
                self.pending = ''
        return lines

    def close(self):
        if self.fd:
            self.fd.close()


class StdinReader:
    """Nonblocking line reader for a pipe or redirected standard input."""

    def __init__(self, fd):
        self.fd = fd
        self.pending = bytearray()
        self.eof = False

    def read(self):
        lines = []
        if self.eof:
            return lines
        # Bound work per refresh so the UI remains responsive under load.
        for _ in range(4):
            if not select.select([self.fd], [], [], 0)[0]:
                break
            try:
                chunk = os.read(self.fd, 65536)
            except BlockingIOError:
                break
            if not chunk:
                self.eof = True
                break
            self.pending.extend(chunk)
            while True:
                pos = self.pending.find(b'\n')
                if pos < 0:
                    break
                lines.append(bytes(self.pending[:pos]).decode('utf-8', 'replace'))
                del self.pending[:pos + 1]
        if self.eof and self.pending:
            lines.append(self.pending.decode('utf-8', 'replace'))
            self.pending.clear()
        return lines


class Monitor:
    MODES = [('url', 'TOP URLS'), ('ip', 'TOP IPS'), ('referer', 'TOP REFERER'),
             ('agent', 'TOP USER AGENTS'), ('status', 'HTTP STATUS'), ('method', 'METHODS'),
             ('4xx', 'TOP 4XX URLS'), ('5xx', 'TOP 5XX URLS'),
             ('slow', 'SLOWEST URLS')]
    SORTS = ('requests', 'bytes', 'avg_time')
    DASHBOARDS = ('OVERVIEW', 'TRAFFIC', 'ERRORS', 'SUCCESS', 'USER AGENTS', 'REFERRERS', 'LATENCY', 'CLIENTS', 'SECURITY', 'BOTS', 'API', 'SLA')

    def __init__(self, args):
        self.args = args
        self.window = args.window
        self.events = deque()
        self.tails = {}
        self.stdin_reader = StdinReader(args.stdin_fd) if getattr(args, "stdin_fd", None) is not None else None
        self.mode = (args.page - 1) if args.page is not None else 0
        self.sort = 0
        self.filter = ''
        self.blacklist = ''
        self.bad_lines = 0
        self.total = 0
        self.started = time.monotonic()
        self.message = ''
        self.next_scan = 0
        self.paused = False
        self.pause_started = None
        self.paused_total = 0.0
        self.colors = False
        self.theme = args.theme
        self.selected = 0
        self.scroll = 0
        self.detail = None  # (source field, selected value, target field)
        self.dashboard = args.dashboard is not None
        self.dashboard_index = self.DASHBOARDS.index(args.dashboard.upper().replace('-', ' ')) if self.dashboard else 0
        self.investigation = False
        self.client_ip = None
        self.client_selected = 0
        self.client_scroll = 0
        self.dashboard_client_selected = 0
        self.history = deque(maxlen=120)  # One sample per virtual second.
        self.last_sample_second = None
        self.frame = 0

    def clock(self):
        """Virtual clock: frozen while paused."""
        real = self.pause_started if self.paused else time.monotonic()
        return real - self.paused_total

    def toggle_pause(self):
        if not self.paused:
            self.pause_started = time.monotonic()
            self.paused = True
        else:
            self.paused_total += time.monotonic() - self.pause_started
            self.pause_started = None
            self.paused = False

    def scan(self):
        paths = set()
        for pattern in self.args.logs:
            if pattern == '-':
                continue
            paths.update(p for p in glob.glob(pattern) if os.path.isfile(p))
        for path in paths - self.tails.keys():
            try:
                self.tails[path] = Tail(path, self.args.from_start)
            except OSError as e:
                self.message = str(e)
        for path in list(self.tails):
            if path not in paths:
                self.tails.pop(path).close()

    def poll(self):
        if self.paused:
            return
        now = self.clock()
        if now >= self.next_scan:
            self.scan()
            self.next_scan = now + 2
        for tail in self.tails.values():
            try:
                for line in tail.read():
                    e = parse(line, now, self.args.referer_domain)
                    if e:
                        self.events.append(e)
                        self.total += 1
                    else:
                        self.bad_lines += 1
            except OSError as exc:
                self.message = str(exc)
        if self.stdin_reader is not None:
            try:
                for line in self.stdin_reader.read():
                    e = parse(line, now, self.args.referer_domain)
                    if e:
                        self.events.append(e)
                        self.total += 1
                    else:
                        self.bad_lines += 1
            except OSError as exc:
                self.message = 'stdin: ' + str(exc)
        self.expire(now)
        self.sample_history(now)

    def expire(self, now):
        while self.events and self.events[0]['t'] < now - self.window:
            self.events.popleft()

    def sample_history(self, now):
        """Refresh per-second chart buckets, including the current second."""
        second = int(now)
        if self.last_sample_second is None:
            self.last_sample_second = second - 1
        start = max(self.last_sample_second + 1, second - 119)
        for tick in range(start, second + 1):
            self.history.append((tick, 0, 0, 0))
        self.last_sample_second = second
        if not self.history:
            return
        # Recalculate the most recent bucket as log lines arrive during a second.
        tick = self.history[-1][0]
        entries = [e for e in self.events if tick <= e['t'] < tick + 1]
        self.history[-1] = (tick, len(entries),
                            sum(e['bytes'] for e in entries),
                            sum(e['request_bytes'] or 0 for e in entries))

    @staticmethod
    def spark(values, width, baseline=False):
        """Render fixed-width ASCII history, with blanks for zero or missing samples."""
        levels = ' .:-=+*#%@'
        if width <= 0:
            return ''
        data = list(values)[-width:]
        if not data:
            return ' ' * width
        maximum = max(max(data), 1)
        marks = ''.join(levels[max(1, min(9, int(v / maximum * 9)))]
                        if v > 0 else ' ' for v in data)
        return marks.rjust(width)

    @staticmethod
    def expand_history(values, width):
        """Stretch a shorter client history across the full graph width.

        Preserve the chronological order; do not invent older zero-value samples.
        """
        data = list(values)
        if not data or width <= 0:
            return []
        if len(data) >= width:
            return data[-width:]
        return [data[min(len(data) - 1, (i * len(data)) // width)]
                for i in range(width)]

    @staticmethod
    def percentile(values, fraction):
        """Nearest-rank percentile of sorted response times in milliseconds."""
        if not values:
            return None
        import math
        return values[max(0, math.ceil(len(values) * fraction) - 1)]

    @staticmethod
    def meter(value, maximum, width=18):
        """ASCII percentage bar with a fixed width."""
        ratio = min(1.0, max(0.0, value / maximum)) if maximum else 0.0
        filled = round(width * ratio)
        return '[' + '#' * filled + '-' * (width - filled) + ']'

    def draw_dashboard(self, stdscr):
        """Live MEOW dashboard with real HTTP metrics and ASCII bars."""
        stdscr.erase()
        h, w = stdscr.getmaxyx()
        if h < 20 or w < 70:
            self.put(stdscr, 0, 0, 'Dashboard needs at least 70x20 (press d to return)')
            stdscr.refresh()
            return

        now = self.clock()
        es = self.matched()
        total = len(es)
        outgoing = sum(e['bytes'] for e in es)
        known_in = [e['request_bytes'] for e in es if e['request_bytes'] is not None]
        incoming = sum(known_in)
        sec = max(1, self.window)
        rps = [sum(e['t'] >= now - span for e in es) / span for span in (1, 5, 60)]
        codes = Counter(e['status'] // 100 for e in es)
        errors = codes[4] + codes[5]
        durations = sorted(e['duration'] * 1000 for e in es if e['duration'] is not None)
        methods = Counter(e['method'].upper() for e in es)
        clients = len({e['ip'] for e in es})
        top_urls = Counter(e['url'] for e in es).most_common(self.args.top)
        top_ips = Counter(e['ip'] for e in es).most_common(self.args.top)
        self.dashboard_clients = top_ips
        self.dashboard_client_selected = min(self.dashboard_client_selected, max(0, len(top_ips)-1))
        spinner = '|/-\\'[self.frame % 4]
        self.frame += 1 if not self.paused else 0
        state = 'PAUSED' if self.paused else 'LIVE'

        self.put(stdscr, 0, 0,
                 f' IPSONIC-ADCTOP v{VERSION} // {self.theme.upper()} // OVERVIEW 1/{len(self.DASHBOARDS)} {spinner} // https://ipsonic.com '.ljust(w - 1),
                 self.color(1, True) if self.colors else curses.A_REVERSE)
        self.put(stdscr, 0, max(0, w - len(state) - 3), f' {state} ',
                 self.color(7 if self.paused else 8, True))
        self.put(stdscr, 1, 0,
                 f' Window {self.window}s | Logs {len(self.tails)} | Requests {total} | '
                 f'Filter {self.filter or "none"} | Blacklist {self.blacklist or "none"}', self.color(2))

        graph_width = max(12, w - 26)
        hist = list(self.history)
        self.put(stdscr, 3, 2, f'REQUESTS / SEC  {rps[0]:.2f}  (5s {rps[1]:.2f} / 60s {rps[2]:.2f})', self.color(9, True))
        self.put(stdscr, 4, 2, 'RPS  [' + self.spark([row[1] for row in hist], graph_width) + ']', self.color(4, True))
        self.put(stdscr, 5, 2, f'TRAFFIC OUT {human(outgoing / sec)}/s', self.color(9, True))
        self.put(stdscr, 6, 2, 'OUT  [' + self.spark([row[2] for row in hist], graph_width) + ']', self.color(3, True))
        self.put(stdscr, 7, 2,
                 f'TRAFFIC IN {human(incoming / sec)}/s' if known_in else
                 'TRAFFIC IN N/A (enable $request_length)', self.color(9, True))
        self.put(stdscr, 8, 2, 'IN   [' +
                 (self.spark([row[3] for row in hist], graph_width) if known_in else ' ' * graph_width) +
                 ']', self.color(2, True))

        # Distinct clients in the currently filtered sliding window.
        self.put(stdscr, 10, 2, f'ACTIVE CLIENTS  {clients} unique IPs', self.color(9, True))
        # Historical distinct IPs are computed from real retained events, per-second.
        ticks = [row[0] for row in hist]
        by_second = defaultdict(set)
        for e in es:
            by_second[int(e['t'])].add(e['ip'])
        self.put(stdscr, 11, 2,
                 'IP/s [' + self.spark(self.expand_history(
                     [len(by_second[t]) for t in ticks[-min(len(ticks), self.window):]],
                     graph_width), graph_width) + ']',
                 self.color(2, True))

        error_pct = 100 * errors / total if total else 0.0
        self.put(stdscr, 13, 2,
                 f'HTTP ERROR RATE  {error_pct:.1f}%  (4xx {codes[4]} / 5xx {codes[5]})',
                 self.color(5 if codes[5] else 4 if errors else 3, True))
        self.put(stdscr, 14, 2, self.meter(errors, total, min(42, w - 18)) +
                 f'  {errors}/{total}', self.color(5 if codes[5] else 4 if errors else 3))

        # Three compact panels share a single row on wide terminals.
        latency = [self.percentile(durations, p) for p in (.50, .95, .99)]
        scale = max(durations) if durations else 0
        order = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS']
        ordered = [(name, methods[name]) for name in order if methods[name]]
        ordered += [(name, n) for name, n in methods.most_common()
                    if name not in order and n]

        # Per-second HTTP error history, respecting active filters and the pause clock.
        by_second_errors = defaultdict(lambda: [0, 0, 0])
        for e in es:
            bucket = by_second_errors[int(e['t'])]
            bucket[0] += 1
            if 400 <= e['status'] < 500:
                bucket[1] += 1
            elif 500 <= e['status'] < 600:
                bucket[2] += 1

        def stacked_methods(y, x, width):
            self.put(stdscr, y, x, 'REQUESTS BY METHOD', self.color(9, True))
            bar_width = max(4, width - 2)
            cursor, remaining = x, bar_width
            for i, (name, n) in enumerate(ordered):
                size = remaining if i == len(ordered) - 1 else min(remaining, round(bar_width * n / total))
                if size:
                    self.put(stdscr, y + 1, cursor, '#' * size,
                             self.color((3, 2, 4, 5, 9, 6, 8)[i % 7], True))
                    cursor += size
                    remaining -= size
            if not ordered:
                self.put(stdscr, y + 1, x, '-' * bar_width, self.color(2))
            for i, (name, n) in enumerate(ordered[:4]):
                self.put(stdscr, y + 2 + i, x, f'{name:<7} {100*n/total:5.1f}%', self.color(2))

        panel_y = 16
        if w >= 105:
            usable = w - 4
            col = usable // 3
            columns = [(2, col - 1), (2 + col, col - 1), (2 + 2 * col, usable - 2 * col)]
            x, width = columns[0]
            self.put(stdscr, panel_y, x, 'RESPONSE TIME (ms)', self.color(9, True))
            for i, (name, value) in enumerate(zip(('P50', 'P95', 'P99'), latency)):
                label = (f'{name} {value:7.1f} ' + self.meter(value, scale, max(3, width - 17))) if value is not None else f'{name} N/A'
                self.put(stdscr, panel_y + 1 + i, x, label, self.color((2, 4, 5)[i], True))
            stacked_methods(panel_y, *columns[1])
            x, width = columns[2]
            self.put(stdscr, panel_y, x, 'HTTP ERROR TRENDS', self.color(9, True))
            ticks = [row[0] for row in hist]
            trend_width = max(4, width - 7)
            self.put(stdscr, panel_y + 1, x,
                     '4XX [' + self.spark([by_second_errors[t][1] for t in ticks], trend_width) + ']',
                     self.color(4, True))
            self.put(stdscr, panel_y + 2, x,
                     '5XX [' + self.spark([by_second_errors[t][2] for t in ticks], trend_width) + ']',
                     self.color(5, True))
            panel_y += 7
        else:
            self.put(stdscr, panel_y, 2, 'RESPONSE TIME (ms)', self.color(9, True))
            for i, (name, value) in enumerate(zip(('P50', 'P95', 'P99'), latency)):
                self.put(stdscr, panel_y + 1 + i, 2,
                         f'{name} {value:.1f} ms' if value is not None else f'{name} N/A',
                         self.color((2, 4, 5)[i], True))
            stacked_methods(panel_y + 5, 2, min(48, w - 4))
            ticks = [row[0] for row in hist]
            self.put(stdscr, panel_y + 12, 2, 'HTTP ERROR TRENDS', self.color(9, True))
            for i, (name, index, color) in enumerate((('4XX', 1, 4), ('5XX', 2, 5))):
                self.put(stdscr, panel_y + 13 + i, 2,
                         name + ' [' + self.spark([by_second_errors[t][index] for t in ticks], max(4, w - 12)) + ']',
                         self.color(color, True))
            panel_y += 16

        # Two prominent configurable rankings share the width above URL lists.
        statuses = Counter(e['status'] for e in es).most_common(self.args.top)
        referers = Counter(e['referer'] for e in es
                           if e['referer'] not in ('-', '')
                           and not any(pattern.search(e['referer'])
                                       for pattern in self.args.dashboard_referer_exclude)).most_common(self.args.top)
        slow = defaultdict(list)
        for e in es:
            if e['duration'] is not None:
                slow[e['url']].append(e['duration'] * 1000)
        slowest = sorted(((url, sum(samples) / len(samples), len(samples))
                          for url, samples in slow.items()),
                         key=lambda item: (-item[1], -item[2]))[:self.args.top]

        def ranking(y, title, entries, color, empty):
            if y >= h - 2:
                return y
            self.put(stdscr, y, 2, title, self.color(9, True))
            y += 1
            if not entries:
                if y < h - 2:
                    self.put(stdscr, y, 2, empty, self.color(2))
                return y + 2
            for entry in entries[:self.args.top]:
                if y >= h - 2:
                    break
                self.put(stdscr, y, 2, entry, self.color(color))
                y += 1
            return y + 1

        status_lines = [f'{status} {count:6d}  ' + self.meter(count, statuses[0][1], max(3, w - 23)) for status, count in statuses]
        client_lines = [f'{count:6d} {ip}' for ip, count in top_ips]

        y = panel_y
        if w >= 88 and y < h - 2:
            left_x, right_x = 2, w // 2
            left_width = right_x - left_x - 2
            right_width = w - right_x - 2
            self.put(stdscr, y, left_x, 'TOP HTTP STATUS ', self.color(9, True))
            self.put(stdscr, y, right_x, 'TOP ACTIVE CLIENTS ', self.color(9, True))
            for i in range(self.args.top):
                if y + 1 + i >= h - 2:
                    break
                if i < len(statuses):
                    status, count = statuses[i]
                    self.put(stdscr, y + 1 + i, left_x,
                             f'{status} {count:6d}  ' + self.meter(count, statuses[0][1], max(3, left_width - 13)),
                             self.color(4))
                if i < len(top_ips):
                    ip, count = top_ips[i]
                    bar = self.meter(count, top_ips[0][1], max(3, right_width - len(ip) - 11))
                    self.put(stdscr, y + 1 + i, right_x,
                             f'{count:6d} {ip} {bar}', self.color(6, True) if i == self.dashboard_client_selected else self.color(3))
            y += self.args.top + 2
        else:
            y = ranking(y, 'TOP HTTP STATUS ', status_lines, 4, 'No responses')
            y = ranking(y, 'TOP ACTIVE CLIENTS ',
                        [('> ' if i == self.dashboard_client_selected else '  ') + line for i, line in enumerate(client_lines)], 3, 'No active clients')

        # Referers and URLs retain full terminal width, one entry per line.
        y = ranking(y, 'TOP REFERERS ',
                    [f'{count:6d}  {name}' for name, count in referers],
                    2, 'No referers')
        y = ranking(y, 'TOP URLS ',
                    [f'{count:6d}  {url}' for url, count in top_urls],
                    3, 'No requests')
        y = ranking(y, 'SLOWEST URLS (avg ms)',
                    [f'{ms:8.1f}ms {count:4d}x  {url}' for url, ms, count in slowest],
                    4, 'N/A (enable $request_time)')

        self.put(stdscr, h - 2, 0,
                 ' IN needs $request_length; latency needs $request_time. Trends respect filters.',
                 self.color(2))
        self.put(stdscr, h - 1, 0,
                 ' UP/DOWN Client | ENTER/i Investigate | d Next Dashboard | SPACE Pause | 1-9 Views | '
                 '+/- Window | r Reset | q Quit '.ljust(w - 1),
                 self.color(1) if self.colors else curses.A_REVERSE)
        stdscr.refresh()

    def draw_special_dashboard(self, stdscr):
        """Focused dashboards; reuse the existing filtered, sliding-window events."""
        stdscr.erase()
        h, w = stdscr.getmaxyx()
        if h < 16 or w < 60:
            self.put(stdscr, 0, 0, 'Dashboard needs at least 60x16 (d: next view)')
            stdscr.refresh()
            return
        es = self.matched()
        now = self.clock()
        sec = max(1, self.window)
        page = self.DASHBOARDS[self.dashboard_index]
        if page == 'ERRORS':
            data = [e for e in es if 400 <= e['status'] < 600]
        elif page == 'SUCCESS':
            data = [e for e in es if 200 <= e['status'] < 400]
        elif page == 'API':
            data = [e for e in es if e['url'].split('?', 1)[0].startswith('/api/') or e['url'].split('?', 1)[0] == '/api']
        else:
            data = es
        count = len(data)
        total = len(es)
        out_bytes = sum(e['bytes'] for e in data)
        known_in = [e['request_bytes'] for e in data if e['request_bytes'] is not None]
        in_bytes = sum(known_in)
        ips = len({e['ip'] for e in data})
        codes = Counter(e['status'] for e in data)
        urls = Counter(e['url'] for e in data)
        clients = Counter(e['ip'] for e in data)
        methods = Counter(e['method'] for e in data)
        durations = sorted(e['duration'] * 1000 for e in data if e['duration'] is not None)
        pct = 100 * count / total if total else 0.0
        spinner = '|/-\\'[self.frame % 4]
        if not self.paused:
            self.frame += 1
        state = 'PAUSED' if self.paused else 'LIVE'
        self.put(stdscr, 0, 0,
                 f' IPSONIC-ADCTOP v{VERSION} // {self.theme.upper()} // {page} {self.dashboard_index+1}/{len(self.DASHBOARDS)} {spinner} // https://ipsonic.com '.ljust(w-1),
                 self.color(1, True) if self.colors else curses.A_REVERSE)
        self.put(stdscr, 0, max(0, w-len(state)-3), f' {state} ',
                 self.color(7 if self.paused else 8, True))
        self.put(stdscr, 1, 0,
                 f' Window {self.window}s | Requests {total} | Filter {self.filter or "none"} | Blacklist {self.blacklist or "none"}',
                 self.color(2))

        if page == 'TRAFFIC':
            title = 'TRAFFIC / BANDWIDTH'
            summary = [f'REQUESTS {count}   RPS {count/sec:.2f}   UNIQUE CLIENTS {ips}',
                       f'OUT {human(out_bytes)}  ({human(out_bytes/sec)}/s)   '
                       + (f'IN {human(in_bytes)} ({human(in_bytes/sec)}/s)' if known_in else 'IN N/A ($request_length)'),
                       f'AVG OUT {human(out_bytes/count) if count else "0B"}/request   '
                       f'P95 {self.percentile(durations, .95):.1f}ms' if durations else
                       f'AVG OUT {human(out_bytes/count) if count else "0B"}/request   P95 N/A']
            graphs = [('RPS', lambda e: 1, 4), ('OUT BYTES', lambda e: e['bytes'], 3),
                      ('IN BYTES', lambda e: e['request_bytes'] or 0, 2)]
            rankings = [('TOP URLS BY OUTGOING BYTES', 'url', 'bytes', 3),
                        ('TOP CLIENTS BY OUTGOING BYTES', 'ip', 'bytes', 6),
                        ('TOP URLS BY REQUEST COUNT', 'url', 'count', 4),
                        ('TOP METHODS', 'method', 'count', 2)]
        elif page == 'ERRORS':
            c4 = sum(v for code, v in codes.items() if 400 <= code < 500)
            c5 = sum(v for code, v in codes.items() if 500 <= code < 600)
            title = 'HTTP ERRORS / 4XX + 5XX'
            summary = [f'ERRORS {count}/{total}   RATE {pct:.1f}%   ERROR RPS {count/sec:.2f}',
                       f'4XX {c4}   5XX {c5}   AFFECTED CLIENTS {ips}',
                       f'ERROR OUT {human(out_bytes)}   P95 '
                       + (f'{self.percentile(durations, .95):.1f}ms' if durations else 'N/A')]
            graphs = [('4XX', lambda e: int(400 <= e['status'] < 500), 4),
                      ('5XX', lambda e: int(500 <= e['status'] < 600), 5),
                      ('ERRORS', lambda e: 1, 9)]
            rankings = [('TOP ERROR URLS', 'url', 'count', 5),
                        ('TOP ERROR CLIENTS', 'ip', 'count', 6),
                        ('HTTP ERROR STATUS CODES', 'status', 'count', 4),
                        ('TOP ERROR METHODS', 'method', 'count', 2)]
        elif page == 'SECURITY':
            suspicious = ('/.env', '/wp-admin', '/wp-login', '/xmlrpc.php', '/.git/',
                          '/admin', '/phpmyadmin', '/cgi-bin/', '/actuator', '/server-status')
            flagged = [e for e in data if e['status'] in (401, 403, 404, 405, 429)
                       or any(token in e['url'].lower() for token in suspicious)]
            title = 'SECURITY / SUSPICIOUS REQUEST ACTIVITY'
            summary = [f'FLAGGED {len(flagged)}/{total}   UNIQUE CLIENTS {len({e["ip"] for e in flagged})}',
                       f'401/403 {codes[401]+codes[403]}   404 {codes[404]}   429 {codes[429]}',
                       'Indicators are heuristic; not proof of an attack']
            flagged_set = {id(e) for e in flagged}
            graphs = [('FLAGGED', lambda e: int(id(e) in flagged_set), 5),
                      ('DENIED', lambda e: int(e['status'] in (401,403)), 4),
                      ('NOT FOUND', lambda e: int(e['status'] == 404), 3)]
            data = flagged
            count = len(data)
            rankings = [('TOP FLAGGED URLS', 'url', 'count', 5),
                        ('TOP FLAGGED CLIENTS', 'ip', 'count', 6),
                        ('FLAGGED STATUS CODES', 'status', 'count', 4),
                        ('TOP USER AGENTS', 'agent', 'count', 2)]
        elif page == 'BOTS':
            signatures = ('bot', 'crawler', 'spider', 'scrapy', 'curl/', 'wget/',
                          'python-requests', 'go-http-client', 'headless', 'httpclient')
            data = [e for e in data if any(t in e['agent'].lower() for t in signatures)]
            count = len(data)
            title = 'BOTS / AUTOMATED CLIENT SIGNATURES'
            summary = [f'MATCHED {count}/{total}   SHARE {100*count/total if total else 0:.1f}%',
                       f'CLIENTS {len({e["ip"] for e in data})}   AGENTS {len({e["agent"] for e in data})}',
                       f'ERRORS {sum(e["status"] >= 400 for e in data)}   OUT {human(sum(e["bytes"] for e in data))}']
            graphs = [('BOT RPS', lambda e: 1, 4),
                      ('BOT ERRORS', lambda e: int(e['status'] >= 400), 5),
                      ('BOT BYTES', lambda e: e['bytes'], 3)]
            rankings = [('TOP BOT USER AGENTS', 'agent', 'count', 3),
                        ('TOP BOT CLIENTS', 'ip', 'count', 6),
                        ('TOP BOT URLS', 'url', 'count', 4),
                        ('BOT STATUS CODES', 'status', 'count', 2)]
        elif page == 'API':
            title = 'API / ENDPOINT ACTIVITY'
            summary = [f'API REQUESTS {count}/{total}   RPS {count/sec:.2f}',
                       f'CLIENTS {ips}   ERRORS {sum(e["status"]>=400 for e in data)}',
                       f'OUT {human(out_bytes)}   P95 '+(f'{self.percentile(durations,.95):.1f}ms' if durations else 'N/A')]
            graphs = [('API RPS', lambda e: 1, 4),
                      ('API ERRORS', lambda e: int(e['status'] >= 400), 5),
                      ('API OUT', lambda e: e['bytes'], 3)]
            rankings = [('TOP API ENDPOINTS', 'url', 'count', 3),
                        ('SLOWEST API ENDPOINTS (AVG MS)', 'url', 'avg_duration', 5),
                        ('TOP API CLIENTS', 'ip', 'count', 6),
                        ('API METHODS', 'method', 'count', 2)]
        elif page == 'SLA':
            timed = len(durations)
            under_100 = sum(e['duration'] is not None and e['duration'] <= .1 for e in data)
            under_500 = sum(e['duration'] is not None and e['duration'] <= .5 for e in data)
            over_1000 = sum(e['duration'] is not None and e['duration'] > 1 for e in data)
            title = 'PERFORMANCE / RESPONSE TIME BUDGETS'
            summary = [f'TIMED {timed}/{total}   <=100ms {under_100}   <=500ms {under_500}',
                       f'>1000ms {over_1000}   P95 '+(f'{self.percentile(durations,.95):.1f}ms' if timed else 'N/A'),
                       f'P99 {self.percentile(durations,.99):.1f}ms' if timed else 'Timing requires $request_time']
            graphs = [('FAST <=100', lambda e: int(e['duration'] is not None and e['duration'] <= .1), 3),
                      ('SLOW >500', lambda e: int(e['duration'] is not None and e['duration'] > .5), 4),
                      ('SLOW >1S', lambda e: int(e['duration'] is not None and e['duration'] > 1), 5)]
            rankings = [('SLOWEST URLS (AVG MS)', 'url', 'avg_duration', 5),
                        ('SLOWEST CLIENTS (AVG MS)', 'ip', 'avg_duration', 6),
                        ('TOP URLS', 'url', 'count', 4),
                        ('HTTP STATUS CODES', 'status', 'count', 2)]
        elif page == 'USER AGENTS':
            title = 'USER AGENTS / CLIENT SOFTWARE'
            agents = Counter(e['agent'] for e in data)
            summary = [f'REQUESTS {count}   DISTINCT AGENTS {len(agents)}   RPS {count/sec:.2f}',
                       f'CLIENTS {ips}   OUT {human(out_bytes)}',
                       f'LEADING AGENT SHARE {100*agents.most_common(1)[0][1]/count:.1f}%' if count else 'LEADING AGENT SHARE N/A']
            graphs = [('AGENT RPS', lambda e: 1, 4), ('OUT BYTES', lambda e: e['bytes'], 3),
                      ('ERRORS', lambda e: int(e['status'] >= 400), 5)]
            rankings = [('TOP USER AGENTS', 'agent', 'count', 3),
                        ('TOP URLS', 'url', 'count', 4),
                        ('TOP CLIENTS', 'ip', 'count', 6),
                        ('HTTP STATUS CODES', 'status', 'count', 2)]
        elif page == 'REFERRERS':
            title = 'REFERRERS / TRAFFIC SOURCES'
            refs = Counter(e['referer'] for e in data if e['referer'] not in ('', '-'))
            summary = [f'REQUESTS {count}   DISTINCT REFERRERS {len(refs)}',
                       f'WITH REFERRER {sum(refs.values())}   WITHOUT {count-sum(refs.values())}',
                       f'CLIENTS {ips}   OUT {human(out_bytes)}']
            graphs = [('RPS', lambda e: 1, 4), ('REFERRERS', lambda e: int(e['referer'] not in ('', '-')), 3),
                      ('ERRORS', lambda e: int(e['status'] >= 400), 5)]
            rankings = [('TOP REFERRERS', 'referer', 'count', 3),
                        ('TOP DESTINATION URLS', 'url', 'count', 4),
                        ('TOP CLIENTS', 'ip', 'count', 6),
                        ('HTTP STATUS CODES', 'status', 'count', 2)]
        elif page == 'LATENCY':
            title = 'RESPONSE TIME / SLOW REQUESTS'
            timed = len(durations)
            summary = [f'REQUESTS {count}   WITH TIMING {timed}   RPS {count/sec:.2f}',
                       f'P50 {self.percentile(durations, .50):.1f}ms   P95 {self.percentile(durations, .95):.1f}ms' if timed else 'P50 N/A   P95 N/A',
                       f'P99 {self.percentile(durations, .99):.1f}ms   MAX {max(durations):.1f}ms' if timed else 'P99 N/A   MAX N/A ($request_time)']
            graphs = [('RPS', lambda e: 1, 4),
                      ('TIME MS', lambda e: (e['duration'] or 0)*1000, 3),
                      ('SLOW >1S', lambda e: int(e['duration'] is not None and e['duration'] > 1), 5)]
            rankings = [('SLOWEST URLS (AVG MS)', 'url', 'avg_duration', 5),
                        ('SLOWEST CLIENTS (AVG MS)', 'ip', 'avg_duration', 6),
                        ('TOP URLS BY REQUEST COUNT', 'url', 'count', 4),
                        ('HTTP STATUS CODES', 'status', 'count', 2)]
        elif page == 'CLIENTS':
            title = 'CLIENT ACTIVITY / REQUEST SOURCES'
            summary = [f'REQUESTS {count}   UNIQUE CLIENTS {ips}   RPS {count/sec:.2f}',
                       f'AVG REQUESTS/CLIENT {count/ips:.1f}' if ips else 'AVG REQUESTS/CLIENT N/A',
                       f'OUT {human(out_bytes)}   ERRORS {sum(v for code,v in codes.items() if code>=400)}']
            graphs = [('RPS', lambda e: 1, 4), ('OUT BYTES', lambda e: e['bytes'], 3),
                      ('ERRORS', lambda e: int(e['status'] >= 400), 5)]
            rankings = [('TOP CLIENTS BY REQUEST COUNT', 'ip', 'count', 6),
                        ('TOP CLIENTS BY OUTGOING BYTES', 'ip', 'bytes', 3),
                        ('TOP REQUESTED URLS', 'url', 'count', 4),
                        ('TOP USER AGENTS', 'agent', 'count', 2)]
        else:
            c2 = sum(v for code, v in codes.items() if 200 <= code < 300)
            c3 = sum(v for code, v in codes.items() if 300 <= code < 400)
            title = 'ALLOWED TRAFFIC / 2XX + 3XX'
            summary = [f'2XX/3XX {count}/{total}   SHARE {pct:.1f}%   RPS {count/sec:.2f}',
                       f'2XX {c2}   3XX {c3}   CLIENTS {ips}',
                       f'OUT {human(out_bytes)} ({human(out_bytes/sec)}/s)   '
                       + (f'IN {human(in_bytes)}' if known_in else 'IN N/A ($request_length)')]
            graphs = [('2XX', lambda e: int(200 <= e['status'] < 300), 3),
                      ('3XX', lambda e: int(300 <= e['status'] < 400), 2),
                      ('OUT BYTES', lambda e: e['bytes'], 4)]
            rankings = [('TOP ALLOWED URLS', 'url', 'count', 3),
                        ('TOP ALLOWED CLIENTS', 'ip', 'count', 6),
                        ('HTTP SUCCESS/REDIRECT CODES', 'status', 'count', 4),
                        ('TOP ALLOWED METHODS', 'method', 'count', 2)]

        self.put(stdscr, 3, 2, title, self.color(9, True))
        for i, line in enumerate(summary):
            self.put(stdscr, 4+i, 2, line, self.color((3, 2, 4)[i], True))

        # Fixed one-second buckets, with no artificial padding of missing samples.
        ticks = [row[0] for row in self.history][-min(self.window, 120):]
        buckets = defaultdict(list)
        for e in data:
            buckets[int(e['t'])].append(e)
        width = max(8, w-19)
        for i, (label, metric, color) in enumerate(graphs):
            values = [sum(metric(e) for e in buckets[t]) for t in ticks]
            self.put(stdscr, 8+i, 2, f'{label:<10} [' + self.spark(values, width) + ']',
                     self.color(color, True))

        # Rankings are always full-width, especially URLs and long User-Agent strings.
        # Distribute available vertical space across sections, prioritizing earlier lists.
        y = 12
        for index, (heading, field, metric, color) in enumerate(rankings):
            if y >= h-3:
                break
            self.put(stdscr, y, 2, heading, self.color(9, True))
            y += 1
            aggregates = Counter()
            durations_by_key = defaultdict(list)
            for e in data:
                if page == 'REFERRERS' and field == 'referer' and e['referer'] in ('', '-'):
                    continue
                if metric == 'avg_duration':
                    if e['duration'] is not None:
                        durations_by_key[e[field]].append(e['duration'] * 1000)
                else:
                    aggregates[e[field]] += e['bytes'] if metric == 'bytes' else 1
            if metric == 'avg_duration':
                entries = sorted(((key, sum(samples)/len(samples)) for key, samples in durations_by_key.items()),
                                 key=lambda pair: -pair[1])
            else:
                entries = aggregates.most_common()
            remaining_sections = len(rankings) - index - 1
            available = min(self.args.top, max(0, h-3-y-remaining_sections*2))
            if not entries and available and y < h-2:
                self.put(stdscr, y, 2, 'No matching requests', self.color(2))
                y += 1
            for name, value in entries[:available]:
                value_text = f'{value:.1f}ms' if metric == 'avg_duration' else (human(value) if metric == 'bytes' else str(value))
                self.put(stdscr, y, 2, f'{value_text:>10}  {name}', self.color(color))
                y += 1
            y += 1

        self.put(stdscr, h-2, 0,
                 ' All metrics respect the current window, filters and pause state.', self.color(2))
        self.put(stdscr, h-1, 0,
                 ' d Next Dashboard / Table | 1-9 Views | F Fail2ban | T Theme | SPACE Pause | '
                 '/ Filter | ! Blacklist | +/- Window | q Quit '.ljust(w-1),
                 self.color(1) if self.colors else curses.A_REVERSE)
        stdscr.refresh()

    def draw_investigation(self, stdscr):
        """Inspect a single client with metrics and its requested URLs."""
        stdscr.erase()
        h, w = stdscr.getmaxyx()
        if h < 17 or w < 65:
            self.put(stdscr, 0, 0, 'Investigation needs at least 65x17 (ESC to return)')
            stdscr.refresh()
            return
        events = [e for e in self.matched() if e['ip'] == self.client_ip]
        count = len(events)
        bytes_out = sum(e['bytes'] for e in events)
        lengths = [e['request_bytes'] for e in events if e['request_bytes'] is not None]
        durations = sorted(e['duration'] * 1000 for e in events if e['duration'] is not None)
        statuses = Counter(e['status'] // 100 for e in events)
        methods = Counter(e['method'] for e in events)
        agents = Counter(e['agent'] for e in events)
        now = self.clock()
        rps = sum(e['t'] >= now - 5 for e in events) / 5.0
        self.put(stdscr, 0, 0, f' IPSONIC-ADCTOP v{VERSION} // {self.theme.upper()} // CLIENT INVESTIGATION // https://ipsonic.com '.ljust(w-1),
                 self.color(1, True) if self.colors else curses.A_REVERSE)
        self.put(stdscr, 1, 2, f'CLIENT {self.client_ip} | WINDOW {self.window}s | {"PAUSED" if self.paused else "LIVE"}', self.color(9, True))
        self.put(stdscr, 3, 2, f'REQUESTS {count:,}    RPS (5s) {rps:.2f}    OUT {human(bytes_out)}    IN {human(sum(lengths)) if lengths else "N/A"}', self.color(3, True))
        self.put(stdscr, 4, 2, f'2XX {statuses[2]}    3XX {statuses[3]}    4XX {statuses[4]}    5XX {statuses[5]}    ERROR RATE {100*(statuses[4]+statuses[5])/count if count else 0:.1f}%', self.color(5 if statuses[5] else 4))
        p50 = self.percentile(durations, .50)
        p95 = self.percentile(durations, .95)
        p99 = self.percentile(durations, .99)
        fmt = lambda n: f'{n:.1f}ms' if n is not None else 'N/A'
        self.put(stdscr, 5, 2, f'RESPONSE TIME  P50 {fmt(p50)}    P95 {fmt(p95)}    P99 {fmt(p99)}', self.color(2))
        self.put(stdscr, 7, 2, 'METHODS  ' + '  '.join(f'{key} {val}' for key, val in methods.most_common(5)), self.color(2))
        self.put(stdscr, 8, 2, 'USER AGENT  ' + (agents.most_common(1)[0][0] if agents else '-'), self.color(2))
        self.put(stdscr, 10, 2, 'REQUESTED URLS  (UP/DOWN to select; live filtered data)', self.color(9, True))
        self.put(stdscr, 11, 2, f'{"REQ":>7} {"4XX":>6} {"5XX":>6} {"AVG ms":>10}  URL'.ljust(w-3), self.color(6, True))
        grouped = defaultdict(lambda: [0, 0, 0, 0.0, 0])
        for e in events:
            g = grouped[e['url']]
            g[0] += 1
            g[1] += (400 <= e['status'] < 500)
            g[2] += (500 <= e['status'] < 600)
            if e['duration'] is not None:
                g[3] += e['duration'] * 1000
                g[4] += 1
        self.client_rows = sorted(grouped.items(), key=lambda x: (-x[1][0], x[0]))
        self.client_selected = min(self.client_selected, max(0, len(self.client_rows)-1))
        visible = max(1, h - 14)
        self.client_scroll = max(0, min(self.client_scroll, max(0, len(self.client_rows)-visible)))
        if self.client_selected < self.client_scroll:
            self.client_scroll = self.client_selected
        if self.client_selected >= self.client_scroll + visible:
            self.client_scroll = self.client_selected - visible + 1
        for index in range(self.client_scroll, min(len(self.client_rows), self.client_scroll+visible)):
            url, g = self.client_rows[index]
            avg = f'{g[3]/g[4]:.1f}' if g[4] else '-'
            line = f'{g[0]:7d} {g[1]:6d} {g[2]:6d} {avg:>10}  {url}'
            self.put(stdscr, 12 + index - self.client_scroll, 2, line.ljust(w-3),
                     self.color(6, True) if index == self.client_selected else self.color(3))
        self.put(stdscr, h-2, 2, f'URLs {len(self.client_rows)} | Selected {self.client_selected+1 if self.client_rows else 0} | Global filters apply', self.color(2))
        self.put(stdscr, h-1, 0, ' ESC Back | UP/DOWN/PgUp/PgDn URLs | SPACE Pause | / Filter | ! Blacklist | q Quit '.ljust(w-1),
                 self.color(1) if self.colors else curses.A_REVERSE)
        stdscr.refresh()

    def investigate(self, ip):
        self.client_ip = ip
        self.client_selected = self.client_scroll = 0
        self.investigation = True

    def matched(self):
        fields = ('url', 'ip', 'referer', 'agent', 'status', 'method')
        include = self.filter.casefold().strip()
        excludes = [term.strip().casefold() for term in self.blacklist.split(',') if term.strip()]
        result = []
        for event in self.events:
            values = [str(event[field]).casefold() for field in fields]
            if include and not any(include in value for value in values):
                continue
            if any(term in value for term in excludes for value in values):
                continue
            result.append(event)
        return result

    def input_filter(self, stdscr, label, current):
        h, w = stdscr.getmaxyx()
        chars = []
        curses.curs_set(1)
        try:
            while True:
                self.put(stdscr, h - 2, 0, (label + ''.join(chars)).ljust(w - 1))
                stdscr.move(h - 2, min(w - 2, len(label) + len(chars)))
                stdscr.refresh()
                key = self.keys.get()
                if key == -1:
                    time.sleep(0.01)
                elif key in (10, 13):
                    return ''.join(chars)
                elif key == 27:
                    return current
                elif key in (8, 127, curses.KEY_BACKSPACE):
                    if chars:
                        chars.pop()
                elif 32 <= key <= 126 and len(chars) < 240:
                    chars.append(chr(key))
        finally:
            curses.curs_set(0)

    @staticmethod
    def put(stdscr, y, x, value, style=0):
        h, w = stdscr.getmaxyx()
        if y < 0 or y >= h or x >= w:
            return
        try:
            stdscr.addnstr(y, x, str(value), max(0, w - x - 1), style)
        except curses.error:
            pass

    def color(self, pair, bold=False):
        style = curses.color_pair(pair) if self.colors else 0
        return style | (curses.A_BOLD if bold else 0)

    def setup_colors(self, stdscr):
        """Apply a full-screen palette, including unused screen cells."""
        self.colors = False
        if not curses.has_colors():
            return
        try:
            curses.start_color()
            theme = THEMES[self.theme]
            is_256 = curses.COLORS >= 256
            palette = theme[1] if is_256 else theme[2]
            background = THEME_BACKGROUNDS[self.theme][0 if is_256 else 1]
            for number, (fg, bg) in enumerate(palette, 1):
                curses.init_pair(number, fg, background if bg is None else bg)
            light_themes = {'daylight', 'irix', 'workbench', 'solar', 'snow', 'arctic', 'paper', 'sapphire', 'atari_st', 'mac_classic', 'mono_white', 'sepia', 'lavender'}
            base_fg = (18 if is_256 else curses.COLOR_BLUE) if self.theme in light_themes else (231 if is_256 else curses.COLOR_WHITE)
            curses.init_pair(11, base_fg, background)
            self.colors = True
            stdscr.bkgd(' ', curses.color_pair(11))
            stdscr.erase()
        except curses.error:
            self.colors = False

    def next_theme(self, stdscr):
        index = (THEME_NAMES.index(self.theme) + 1) % len(THEME_NAMES)
        self.theme = THEME_NAMES[index]
        self.setup_colors(stdscr)
        self.message = f'Theme: {self.theme} - {THEMES[self.theme][0]}'
        stdscr.clear()

    def rows(self, events):
        """Build grouped rows for the active view or drill-down."""
        if self.detail:
            source, value, key = self.detail
            events = [e for e in events if e[source] == value]
            title = f'{source.upper()}: {value}  ->  TOP {key.upper()}'
        else:
            key, title = self.MODES[self.mode]
            if key == '4xx':
                events = [e for e in events if 400 <= e['status'] <= 499]
                key = 'url'
            elif key == '5xx':
                events = [e for e in events if 500 <= e['status'] <= 599]
                key = 'url'
            elif key == 'slow':
                # Only measured response times can rank slow URLs.
                events = [e for e in events if e['duration'] is not None]
                key = 'url'
        groups = defaultdict(lambda: [0, 0, 0.0, 0])
        for e in events:
            g = groups[str(e[key])]
            g[0] += 1
            g[1] += e['bytes']
            if e['duration'] is not None:
                g[2] += e['duration'] * 1000
                g[3] += 1

        def sortkey(item):
            _, g = item
            if not self.detail and self.mode == 8:
                return (g[2] / g[3] if g[3] else -1, g[0])
            if self.sort == 0:
                return (g[0], g[1])
            if self.sort == 1:
                return (g[1], g[0])
            return (g[2] / g[3] if g[3] else -1, g[0])

        return key, title, sorted(groups.items(), key=sortkey, reverse=True)

    def move_selection(self, delta, visible):
        self.selected = max(0, min(self.selected + delta, max(0, len(self.current_rows) - 1)))
        if self.selected < self.scroll:
            self.scroll = self.selected
        if self.selected >= self.scroll + visible:
            self.scroll = self.selected - visible + 1

    def open_detail(self):
        if self.detail or not self.current_rows:
            return
        source, _ = self.MODES[self.mode]
        if source in ('4xx', '5xx', 'slow'):
            source = 'url'
        target = {'referer': 'url', 'ip': 'url', 'url': 'ip'}.get(source)
        if target and self.selected < len(self.current_rows):
            self.detail = (source, self.current_rows[self.selected][0], target)
            self.selected = 0
            self.scroll = 0

    def draw(self, stdscr):
        stdscr.erase()
        h, w = stdscr.getmaxyx()
        if h < 16 or w < 65:
            self.put(stdscr, 0, 0, 'Terminal must be at least 65x16')
            stdscr.refresh()
            return

        now = self.clock()
        es = self.matched()
        count = len(es)
        bytes_total = sum(e['bytes'] for e in es)
        counts = Counter(e['status'] // 100 for e in es)
        rps = [sum(e['t'] >= now - span for e in es) / span
               for span in (1, 5, 60)]
        state = 'PAUSED' if self.paused else 'LIVE'
        banner = f' IPSONIC-ADCTOP v{VERSION} [{self.theme.upper()}] // 1 URLs 2 IPs 3 REFERER 4 AGENTS 5 STATUS 6 METHODS 7 4XX 8 5XX 9 SLOW // https://ipsonic.com '
        self.put(stdscr, 0, 0, banner.ljust(w - 1),
                 self.color(1, True) if self.colors else curses.A_REVERSE | curses.A_BOLD)
        # Highlight the active numbered view with a contrasting background.
        # Only paint the visible part of the menu; the right-hand LIVE badge wins.
        active_label = ('1 URLs', '2 IPs', '3 REFERER', '4 AGENTS',
                        '5 STATUS', '6 METHODS', '7 4XX', '8 5XX', '9 SLOW')[self.mode]
        if not self.detail:
            pos = banner.find(active_label)
            state_pos = max(0, w - len(state) - 3)
            if 0 <= pos < min(w - 1, state_pos):
                self.put(stdscr, 0, pos, active_label[:max(0, state_pos - pos)],
                         self.color(10, True) if self.colors else curses.A_STANDOUT | curses.A_BOLD)
        self.put(stdscr, 0, max(0, w - len(state) - 3), f' {state} ',
                 self.color(7 if self.paused else 8, True))
        self.put(stdscr, 1, 0,
                 f'Logs: {len(self.tails)} | Window: {self.window}s | '
                 f'Requests: {count} | Total since start: {self.total} | '
                 f'Invalid lines: {self.bad_lines}', self.color(2))
        self.put(stdscr, 2, 0,
                 f'RPS 1s: {rps[0]:.2f}  5s: {rps[1]:.2f}  60s: {rps[2]:.2f} | '
                 f'Traffic: {human(bytes_total)} | '
                 f'Bandwidth: {human(bytes_total / self.window)}/s', self.color(3, True))

        x = 0
        for label, number, pair in (
            ('2xx', counts[2], 3), ('3xx', counts[3], 2),
            ('4xx', counts[4], 4), ('5xx', counts[5], 5)
        ):
            fragment = f'{label}: {number}  '
            self.put(stdscr, 3, x, fragment, self.color(pair, True))
            x += len(fragment)
        filter_text = f'| Filter: {self.filter or "(none)"}  '
        self.put(stdscr, 3, x, filter_text, self.color(4 if self.filter else 2))
        x += len(filter_text)
        self.put(stdscr, 3, x, f'| Blacklist (!): {self.blacklist or "(none)"}',
                 self.color(5 if self.blacklist else 2, bool(self.blacklist)))

        key, title, rows = self.rows(es)
        self.current_rows = rows
        self.selected = min(self.selected, max(0, len(rows) - 1))
        visible = max(1, h - 8)
        self.scroll = max(0, min(self.scroll, max(0, len(rows) - visible)))
        if self.selected < self.scroll:
            self.scroll = self.selected
        elif self.selected >= self.scroll + visible:
            self.scroll = self.selected - visible + 1

        self.put(stdscr, 4, 0, f'{title} | Sort by: {"avg_time" if not self.detail and self.mode == 8 else self.SORTS[self.sort]} | '
                 f'Entries: {len(rows)} | Selected: {self.selected + 1 if rows else 0}',
                 self.color(9, True))
        header = f'{"REQ":>7} {"BYTES":>11} {"AVG ms":>10}  ENTRY'
        self.put(stdscr, 5, 0, header.ljust(w - 1),
                 self.color(6, True) if self.colors else curses.A_REVERSE)

        for y, (name, g) in enumerate(rows[self.scroll:self.scroll + visible], 6):
            index = self.scroll + y - 6
            avg = f'{g[2] / g[3]:.1f}' if g[3] else '-'
            prefix = f'{g[0]:7d} {human(g[1]):>11} {avg:>10}  '
            pair = 2
            if key == 'status':
                try:
                    pair = {2: 3, 3: 2, 4: 4, 5: 5}.get(int(name) // 100, 2)
                except ValueError:
                    pass
            if index == self.selected:
                self.put(stdscr, y, 0, (prefix + name).ljust(w - 1),
                         self.color(6, True) if self.colors else curses.A_REVERSE)
            else:
                self.put(stdscr, y, 0, prefix, self.color(3))
                self.put(stdscr, y, len(prefix), name, self.color(pair))

        self.put(stdscr, h - 2, 0, self.message, self.color(5))
        footer = (' d Next Dashboard | i Investigate | UP/DOWN Select | ENTER Details | ESC Back | SPACE Pause | '
                  '1-9 Views | F Fail2ban | T Theme | s Sort | / Filter | ! Blacklist | c/x Clear | +/- Window | r Reset | q Quit ')
        self.put(stdscr, h - 1, 0, footer.ljust(w - 1),
                 self.color(1) if self.colors else curses.A_REVERSE)
        stdscr.refresh()

    def fail2ban_recommendations(self):
        """Suggest review-only failregex patterns; no bans or filter changes."""
        from collections import Counter
        from urllib.parse import urlsplit
        suspicious = Counter()
        for e in self.events:
            if e['status'] in (401, 403, 404, 429):
                path = urlsplit(e['url']).path
                if path:
                    suspicious[path] += 1
        common = suspicious.most_common(1)
        path = common[0][0] if common else '/wp-login.php'
        # Escape log-derived paths so they cannot inject regex operators.
        escaped = re.escape(path)
        prefix = r'^<HOST> - .*?\\[[^\\]]+\\] "[A-Z]+ '
        suffix = r'(?:\\?[^ "]*|) HTTP/[0-9.]+" (?:401|403|404|429) '
        return [
            ('Repeated 4xx on observed URL', prefix + escaped + suffix),
            ('Authentication failures (401/403)', r'^<HOST> - .*?\\[[^\\]]+\\] "[A-Z]+ [^"]+ HTTP/[0-9.]+" (?:401|403) '),
            ('Missing resources / scans (404)', r'^<HOST> - .*?\\[[^\\]]+\\] "[A-Z]+ [^"]+ HTTP/[0-9.]+" 404 '),
            ('Rate-limited requests (429)', r'^<HOST> - .*?\\[[^\\]]+\\] "[A-Z]+ [^"]+ HTTP/[0-9.]+" 429 '),
        ]

    def show_fail2ban_suggestions(self, stdscr):
        """Read-only help screen; 1-4 select a suggested failregex, ESC returns."""
        selected = 0
        while True:
            h, w = stdscr.getmaxyx()
            stdscr.erase()
            patterns = self.fail2ban_recommendations()
            lines = [
                ' FAIL2BAN FILTER SUGGESTIONS — REVIEW ONLY / NO BANS',
                '',
                ' Suggested failregex for Nginx combined access logs:',
                ' 1  ' + patterns[0][0],
                ' 2  ' + patterns[1][0],
                ' 3  ' + patterns[2][0],
                ' 4  ' + patterns[3][0],
                '',
                ' Selected: ' + patterns[selected][0],
                ' [Definition]',
                ' failregex = ' + patterns[selected][1],
                ' ignoreregex =',
                '',
                ' IMPORTANT: these are candidates, NOT validated security rules.',
                ' One matching line is not proof of abuse; tune findtime/maxretry.',
                ' Assumes client IP begins each Nginx combined-log line.',
                ' Check your real log_format, reverse-proxy IP and trusted proxies.',
                ' Validate with fail2ban-regex before enabling any jail.',
                '',
                ' 1-4 Choose pattern | ESC / f Return | q Quit ADCTOP',
            ]
            y = 0
            for line in lines:
                if y >= h - 1:
                    break
                # Wrap long regex for narrower terminals.
                while line and y < h - 1:
                    self.put(stdscr, y, 0, line[:max(1, w-1)],
                             self.color(1) if y == 0 else self.color(2))
                    line = line[max(1, w-1):]
                    y += 1
                if not line and y < h-1:
                    y += 1
            stdscr.refresh()
            key = self.keys.get()
            if key == -1:
                time.sleep(0.025)
                continue
            if key in (27, ord('f'), ord('F')):
                return False
            if key in (ord('q'), ord('Q')):
                return True
            if ord('1') <= key <= ord('4'):
                selected = key - ord('1')

    def run(self, stdscr):
        self.setup_colors(stdscr)
        self.current_rows = []
        curses.curs_set(0)
        # ncurses renders the UI; it never reads keyboard input.
        self.keys = TerminalKeys()
        stdscr.nodelay(True)
        stdscr.timeout(0)
        if hasattr(curses, 'set_escdelay'):
            curses.set_escdelay(25)
        try:
            while True:
                self.poll()
                if self.investigation:
                    self.draw_investigation(stdscr)
                elif self.dashboard:
                    if self.dashboard_index == 0:
                        self.draw_dashboard(stdscr)
                    else:
                        self.draw_special_dashboard(stdscr)
                else:
                    self.draw(stdscr)
                # Read the keyboard continuously, independently of log input.
                # A key pressed during rendering remains queued for this loop.
                deadline = time.monotonic() + self.args.interval
                while time.monotonic() < deadline:
                    key = self.keys.get()
                    if key == -1:
                        time.sleep(min(0.01, max(0.0, deadline - time.monotonic())))
                        continue
                    if key in (ord('f'), ord('F')):
                        if self.show_fail2ban_suggestions(stdscr):
                            return
                    elif key in (ord('t'), ord('T')):
                        self.next_theme(stdscr)
                    elif key in (ord('q'), ord('Q')):
                        return
                    if key in (ord('i'), ord('I')):
                        if self.investigation:
                            self.investigation = False
                        elif self.dashboard:
                            clients = getattr(self, 'dashboard_clients', [])
                            if clients:
                                self.investigate(clients[self.dashboard_client_selected][0])
                            else:
                                self.message = 'No clients in current window'
                        elif self.mode == 1 and not self.detail and self.current_rows:
                            self.investigate(self.current_rows[self.selected][0])
                    elif key in (ord('d'), ord('D')):
                        self.investigation = False
                        if self.dashboard:
                            self.dashboard_index += 1
                            if self.dashboard_index >= len(self.DASHBOARDS):
                                self.dashboard_index = 0
                                self.dashboard = False
                        else:
                            self.dashboard_index = 0
                            self.dashboard = True
                        self.selected = 0
                        self.scroll = 0
                    elif key == ord(' '):
                        self.toggle_pause()
                    elif ord('1') <= key <= ord('9'):
                        self.mode = key - ord('1')
                        self.dashboard = False
                        self.investigation = False
                        self.detail = None
                        self.selected = self.scroll = 0
                    elif key == curses.KEY_UP:
                        if self.investigation:
                            self.client_selected = max(0, self.client_selected - 1)
                        elif self.dashboard:
                            self.dashboard_client_selected = max(0, self.dashboard_client_selected-1)
                        else:
                            self.move_selection(-1, max(1, stdscr.getmaxyx()[0] - 8))
                    elif key == curses.KEY_DOWN:
                        if self.investigation:
                            self.client_selected = min(max(0, len(self.client_rows)-1), self.client_selected + 1)
                        elif self.dashboard:
                            self.dashboard_client_selected = min(max(0, len(getattr(self, 'dashboard_clients', []))-1), self.dashboard_client_selected+1)
                        else:
                            self.move_selection(1, max(1, stdscr.getmaxyx()[0] - 8))
                    elif key == curses.KEY_PPAGE:
                        if self.investigation:
                            self.client_selected = max(0, self.client_selected - max(1, stdscr.getmaxyx()[0]-14))
                        else:
                            self.move_selection(-max(1, stdscr.getmaxyx()[0] - 8), max(1, stdscr.getmaxyx()[0] - 8))
                    elif key == curses.KEY_NPAGE:
                        if self.investigation:
                            self.client_selected = min(max(0, len(self.client_rows)-1), self.client_selected + max(1, stdscr.getmaxyx()[0]-14))
                        else:
                            self.move_selection(max(1, stdscr.getmaxyx()[0] - 8), max(1, stdscr.getmaxyx()[0] - 8))
                    elif key in (10, 13, curses.KEY_ENTER):
                        if not self.investigation and not self.dashboard and self.mode == 1 and not self.detail and self.current_rows:
                            self.investigate(self.current_rows[self.selected][0])
                        elif self.dashboard and getattr(self, 'dashboard_clients', []):
                            self.investigate(self.dashboard_clients[self.dashboard_client_selected][0])
                        elif not self.investigation and not self.dashboard and self.detail and self.current_rows:
                            # URL -> IP drill-down: open the same client investigation
                            # used by the main IP view (2), preserving the drill-down.
                            source, value, target = self.detail
                            if target == 'ip' and self.selected < len(self.current_rows):
                                self.investigate(self.current_rows[self.selected][0])
                        elif not self.investigation:
                            self.open_detail()
                    elif key == 27:
                        if self.investigation:
                            # Return to the originating list, including URL -> IP.
                            self.investigation = False
                        else:
                            self.detail = None
                            self.selected = self.scroll = 0
                    elif key == ord('s'):
                        self.selected = self.scroll = 0
                        self.sort = (self.sort + 1) % len(self.SORTS)
                    elif key == ord('c'):
                        self.filter = ''
                    elif key == ord('r'):
                        self.events.clear()
                        self.history.clear()
                        self.last_sample_second = None
                        self.total = 0
                        self.bad_lines = 0
                    elif key == ord('+'):
                        self.window = min(3600, self.window + 30)
                    elif key == ord('-'):
                        self.window = max(10, self.window - 30)
                    elif key == ord('x'):
                        self.blacklist = ''
                    elif key == ord('/'):
                        self.filter = self.input_filter(stdscr, 'Filter: ', self.filter)
                    elif key == ord('!'):
                        self.blacklist = self.input_filter(stdscr, 'Blacklist (comma-separated): ', self.blacklist)
                    # Redraw immediately after a real keypress; do not throttle
                    # keyboard handling or require another press to see changes.
                    break
        finally:
            self.keys.close()
            for tail in self.tails.values():
                tail.close()


def main():
    ap = argparse.ArgumentParser(
        description='IPSONIC-ADCTOP: live nginx combined access log monitor',
        epilog=CLI_DISCLAIMER,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    ap.add_argument('-V', '--version', action='version',
                    version=f'IPSONIC-ADCTOP v{VERSION}\n\n{CLI_DISCLAIMER}')
    ap.add_argument('logs', nargs='*', help='Log files/globs, or - for stdin (default: stdin when piped, otherwise /var/log/nginx/access.log)')
    ap.add_argument('--input-fd', type=int, metavar='FD',
                    help='Read nginx log lines from an inherited file descriptor (e.g. --input-fd 3 with 3<&0 0</dev/tty)')
    ap.add_argument('-w', '--window', type=int, default=60, help='Time window in seconds')
    ap.add_argument('-i', '--interval', type=float, default=0.5, help='Refresh interval in seconds')
    ap.add_argument('--from-start', action='store_true', help='Read existing log lines at startup (using ingestion time)')
    startup = ap.add_mutually_exclusive_group()
    startup.add_argument('-d', '--dashboard', nargs='?', const='overview', default=None,
                         choices=[name.lower().replace(' ', '-') for name in Monitor.DASHBOARDS],
                         metavar='NAME', help='Start dashboard by name (default: overview); d cycles dashboards then Table')
    startup.add_argument('-p', '--page', type=int, choices=range(1, 10), metavar='1-9',
                         help='Start directly on a numbered page (1 through 9)')
    ap.add_argument('--theme', choices=THEME_NAMES, default='meow',
                    help='Color theme: ' + '; '.join(f'{name} ({THEMES[name][0]})' for name in THEME_NAMES) + '; default: meow; cycle with T')
    ap.add_argument('--list-dashboards', action='store_true', help='List dashboard names and exit')
    ap.add_argument('--list-themes', action='store_true',
                    help='List available color themes and exit')
    ap.add_argument('--top', type=int, default=5, metavar='N',
                    help='Maximum entries per dashboard top list (default: 5)')
    ap.add_argument('--referer-domain', action='store_true', help='Group referrers by hostname')
    ap.add_argument('--dashboard-referer-exclude', action='append', default=[], metavar='REGEX',
                    help='Exclude matching referers from dashboard TOP REFERERS only; repeatable, case-insensitive regex')
    args = ap.parse_args()
    if args.list_dashboards:
        for name in Monitor.DASHBOARDS:
            print(name.lower().replace(' ', '-'))
        return
    if args.list_themes:
        for name, (description, _, _) in THEMES.items():
            print(f'{name:10} {description}' + (' [default]' if name == 'meow' else ''))
        return
    try:
        args.dashboard_referer_exclude = [re.compile(pattern, re.IGNORECASE)
                                         for pattern in args.dashboard_referer_exclude]
    except re.error as exc:
        ap.error(f'invalid --dashboard-referer-exclude regex: {exc}')
    if args.window <= 0 or args.interval <= 0 or args.top <= 0:
        ap.error('window, interval and top must be > 0')
    if args.input_fd is not None and args.input_fd < 0:
        ap.error('--input-fd must be a non-negative file descriptor')
    if args.input_fd is not None and '-' in args.logs:
        ap.error('use either --input-fd FD or - as a log source, not both')
    stdin_requested = '-' in args.logs or (not args.logs and args.input_fd is None
                                            and not sys.stdin.isatty())
    if not args.logs:
        args.logs = [] if args.input_fd is not None else (
            ['-'] if stdin_requested else ['/var/log/nginx/access.log'])
    args.stdin_fd = None
    tty_fd = None
    if args.input_fd is not None:
        try:
            # Duplicate so the caller retains ownership of the inherited FD.
            args.stdin_fd = os.dup(args.input_fd)
        except OSError as exc:
            ap.error(f'cannot use --input-fd {args.input_fd}: {exc}')
    elif stdin_requested:
        args.stdin_fd = os.dup(0)
    if not os.isatty(0):
        # Keep curses keyboard input independent of the nginx log stream.
        try:
            tty_fd = os.open('/dev/tty', os.O_RDWR)
            os.dup2(tty_fd, 0)
        except OSError:
            if args.stdin_fd is not None:
                os.close(args.stdin_fd)
            ap.error('interactive mode requires a controlling terminal (/dev/tty)')
    try:
        # Python's sys.stdin may still reference the original pipe after dup2.
        # Rebind it to the terminal before ncurses initializes.
        if tty_fd is not None:
            sys.stdin = os.fdopen(os.dup(0), 'r', buffering=1, closefd=True)
        curses.wrapper(Monitor(args).run)
    except KeyboardInterrupt:
        pass
    finally:
        if args.stdin_fd is not None:
            os.close(args.stdin_fd)
        if tty_fd is not None:
            os.close(tty_fd)


if __name__ == '__main__':
    main()
